Solved

Returning Stored procedure from SQL via ODBC

Posted on 2013-05-24
1
296 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
Comment Utility
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Suggested Solutions

I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

763 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

12 Experts available now in Live!

Get 1:1 Help Now