Solved

Returning Stored procedure from SQL via ODBC

Posted on 2013-05-24
1
305 Views
Last Modified: 2013-06-18
Hello,

I have a very big application here that connects to a mySQL database. For each stored procedure in the server, there is a function or sub in the application. The problem is that I needed to migrate this database to a SQL Server, so all the routines that were made for mySQL need to be adapted...

The app has a function to "ExecuteReader" and a "ExecuteCommand" to read tables and send commands with no return.

The ExecuteReader function is like this:
    Function ExecuteReader(ByVal SQLString As String, Optional ByVal Parameters As List(Of OdbcParameter) = Nothing) As DataTable

        SyncLock GlobalCommandBridge

            'Function that executes a command bringing back results, usually used for selects. It returns all results as a Datatable.
            Dim Result As DataTable = New DataTable

            'bridgeCommand = New OdbcCommand("USE " & Database & ";", bridgeConnection)

            Try
                bridgeCommand.Parameters.Clear()
                'If Not ConnectionState = Data.ConnectionState.Open Then OpenConnection()
                'bridgeCommand.ExecuteNonQuery()

                'Set Parameters if there are any
                If Not Parameters Is Nothing Then
                    For Each Parameter As OdbcParameter In Parameters
                        bridgeCommand.Parameters.Add(Parameter)
                    Next
                End If

                If bridgeConnection.State = ConnectionState.Open Then

                    'Executes the command and gather the result back to the Result Datatable object.
                    bridgeCommand.CommandText = Replace(SQLString, "\", "\\")
                    'Application.DoEvents()
                    Result.Load(bridgeCommand.ExecuteReader)
                    Return Result

                Else

                    'If the connection couldn't be opened, returns nothing.
                    Return Nothing

                End If

            Catch ex As Exception

                MsgBox(ex.Message)
                Return Nothing

            End Try

        End SyncLock

    End Function

Open in new window



And an example of stored procedure call from app is this:
    Public Function prcUsrReturnSystem(ByVal id_System As Integer) As DataTable

        'Returns user system details based on it's ID.
        Dim Parameters As New List(Of OdbcParameter)
        Dim AddingParameter As OdbcParameter
        AddingParameter = New OdbcParameter("@id_System", OdbcType.Int, Len(id_System), "id_System")
        AddingParameter.Value = id_System
        Parameters.Add(AddingParameter)

        Return ExecuteReader("exec dbMe..prcUsrReturnSystem (?);", Parameters)

    End Function

Open in new window



Since there are tons of functions like this, I rather adapt the "ExecuteReader" function to work without changing them... One thing I noticed is that parameters in SQL Server have an "@" in front of them... I was trying to make it work but got the error:

ERROR [42000] [Microsoft][SQL Server Native Client 10.0][SQL Server]Incorrect syntax near '@P1

Open in new window


Any ideas??


thanks a lot
0
Comment
Question by:toddinho
1 Comment
 
LVL 13

Accepted Solution

by:
Alexander Eßer [Alex140181] earned 500 total points
ID: 39195462
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

838 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question