问题
I'm a total newb to stored procedures, so I may be missing something easy, but I researched the basics and I'm stuck trying to integrate it from vb.net code. I created a simple stored procedure (I think) that just runs a query of data for today's results:
-- Routine DDL -- Note: comments before and after the routine body will not be stored by the server
DELIMITER $$
CREATE DEFINER=`root`@`%` PROCEDURE `GetRuntestToday`()
BEGIN
Select * From runtest.runtest_records where
Test_Date=CURDATE()
order by test_date desc, Convert(test_time_stop,DECIMAL(5,2)) desc;
END
When I log on to the main MySql database and attempt to run it from the MySql prompt, it appears to work fine. I simply type call runtest.GetRuntestToday();
and it returns 59 rows of data in command prompt text form.
I wrote a VB.net program to try to get that same data but I keep getting an error. The error exception details are:
System.Data.Odbc.OdbcException was unhandled
ErrorCode=-2146232009
Message="ERROR [42000] [MySQL][ODBC 5.1 Driver][mysqld-5.1.51-community-log]You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'GetRuntestToday' at line 1"
Source="myodbc5.dll"
StackTrace:
at System.Data.Odbc.OdbcConnection.HandleError(OdbcHandle hrHandle, RetCode retcode)
at System.Data.Odbc.OdbcCommand.ExecuteReaderObject(CommandBehavior behavior, String method, Boolean needReader, Object[] methodArguments, SQL_API odbcApiMethod)
at System.Data.Odbc.OdbcCommand.ExecuteReaderObject(CommandBehavior behavior, String method, Boolean needReader)
at System.Data.Odbc.OdbcCommand.ExecuteReader(CommandBehavior behavior)
at System.Data.Odbc.OdbcCommand.ExecuteReader()
at MySqlHelper.mMain.DoMyStoredProcedure() in C:\vss\MySqlHelper\MySqlHelper\mMain.vb:line 2642
at MySqlHelper.mMain.Main() in C:\vss\MySqlHelper\MySqlHelper\mMain.vb:line 29
at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
InnerException:
The code I am running is:
Public Sub DoMyStoredProcedure()
Dim MyConString As String = "My String Details"
Dim dbcRuntest As New OdbcConnection(MyConString)
Dim cmd As New OdbcCommand
Dim reader As OdbcDataReader
cmd.CommandText = "GetRuntestToday"
cmd.CommandType = CommandType.StoredProcedure
cmd.Connection = dbcRuntest
dbcRuntest.Open()
reader = cmd.ExecuteReader()
dbcRuntest.Close()
End Sub
The error happens on the line:
reader = cmd.ExecuteReader()
What am I missing? I don't see any syntax problems and the stored procedure works from the command prompt. I've played around a little with the DELIMITER stuff making it // instead of $$ but nothing seems to fix it.
回答1:
You might try changing to a direct call (I haven't tested this:
cmd.CommandType = CommandType.Text
cmd.CommandText = "CALL GetRuntestToday"
Also, a better way of writing your code:
Dim MyConString As String = "My String Details"
Using dbcRuntest As New OdbcConnection(MyConString)
dbcRuntest.Open()
Using cmd As New OdbcCommand("CALL GetRuntestToday", dbcRuntest)
cmd.CommandType = CommandType.Text
Using reader As OdbcDataReader = cmd.ExecuteReader
'do someting with reader'
End Using
End Using
End Using
The Using
structure automatically closes the connection and disposes it (along with the other objects).
Edited to use CALL instead of EXECUTE
来源:https://stackoverflow.com/questions/11781793/vb-net-to-mysql-stored-procedure-error