Solved

Returning Stored procedure from SQL via ODBC

Posted on 2013-05-24
1
299 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

912 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now