Solved

Returning Stored procedure from SQL via ODBC

Posted on 2013-05-24
1
310 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 13

Accepted Solution

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

Featured Post

Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

Question has a verified solution.

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

Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

705 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