1

我是存储过程的新手,所以我可能会遗漏一些简单的东西,但我研究了基础知识,并且试图将它与 vb.net 代码集成。我创建了一个简单的存储过程(我认为),它只为今天的结果运行数据查询:


-- 例程 DDL -- 注意:例程正文前后的注释不会被服务器存储


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

当我登录到主 MySql 数据库并尝试从 MySql 提示符运行它时,它似乎工作正常。我只需键入call runtest.GetRuntestToday();,它就会以命令提示符文本形式返回 59 行数据。

我编写了一个 VB.net 程序来尝试获取相同的数据,但我不断收到错误消息。错误异常详细信息是:

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: 

我正在运行的代码是:

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

错误发生在线:

reader = cmd.ExecuteReader()

我错过了什么?我没有看到任何语法问题,并且存储过程可以在命令提示符下运行。我已经玩了一些 DELIMITER 的东西使它//而不是 $$ 但似乎没有什么可以解决它。

4

1 回答 1

2

您可以尝试更改为直接调用(我还没有测试过这个:

 cmd.CommandType = CommandType.Text
 cmd.CommandText = "CALL GetRuntestToday"

此外,编写代码的更好方法:

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

Using结构会自动关闭连接并处理它(连同其他对象)。

编辑为使用 CALL 而不是 EXECUTE

于 2012-08-02T17:19:15.527 回答