“命令执行期间遇到致命错误”我正在使用SET和SELECT xxx FROM(SELECT xxx FROM xxx)

时间:2015-08-28 01:56:27

标签: mysql vb.net

我有这段代码:

Function Get_Control_Station_Address(counter As Integer)
    Check_DB_Con() 'Check if the connection is okay
    SQL_Query = "SET @row_number = 0; " _
                & "SELECT hardware_add " _
                & "FROM (" _
                    & "SELECT " _
                    & "@row_number:=@row_number + 1 AS num, " _
                    & "hardware_add AS hardware_add " _
                    & "FROM teller_info" _
                & ") AS sub_query " _
                & "WHERE num = " & counter & ";"

    Dim MySQL_CMD As New MySqlCommand(SQL_Query, MysqlConn)

    Try
        MySQL_CMD.Connection.Open()
        MySQL_Reader = MySQL_CMD.ExecuteReader()

        While MySQL_Reader.Read
            MySQL_Result = MySQL_Reader("hardware_add")
        End While

        Return MySQL_Result
        MySQL_Reader.Close()
    Catch myerror As MySqlException
        Console.WriteLine("Failed to run query: " & myerror.Message)
        Return Nothing
    Finally
        MysqlConn.Close()
        MysqlConn.Dispose()
    End Try
End Function

我收到此错误:

A first chance exception of type 'MySql.Data.MySqlClient.MySqlException' occurred in MySql.Data.dll
A first chance exception of type 'MySql.Data.MySqlClient.MySqlException' occurred in MySql.Data.dll
A first chance exception of type 'MySql.Data.MySqlClient.MySqlException' occurred in MySql.Data.dll
A first chance exception of type 'MySql.Data.MySqlClient.MySqlException' occurred in MySql.Data.dll
A first chance exception of type 'MySql.Data.MySqlClient.MySqlException' occurred in MySql.Data.dll
A first chance exception of type 'MySql.Data.MySqlClient.MySqlException' occurred in MySql.Data.dll
Failed to run query: Fatal error encountered during command execution.

我确信这里有 no basic errors ,例如database connection error,作为证明我有这个功能,我100%肯定能够工作,它几乎与以前的功能不起作用,唯一的区别是查询。

Function Get_Control_Station_Total()
    Check_DB_Con() 'Check if the connection is okay
    SQL_Query = "SELECT COUNT(*) AS total_count FROM teller_info"
    Dim MySQL_CMD As New MySqlCommand(SQL_Query, MysqlConn)

    Try
        MySQL_CMD.Connection.Open()
        MySQL_Reader = MySQL_CMD.ExecuteReader()

        While MySQL_Reader.Read
            MySQL_Result = MySQL_Reader("total_count")
        End While

        Return MySQL_Result
        MySQL_Reader.Close()
    Catch myerror As MySqlException

        Return ("Failed to run query: " & myerror.Message)
    Finally
        MysqlConn.Close()
        MysqlConn.Dispose()
    End Try
End Function

所以仔细观察这个问题似乎是这个代码是错误的根源。

SQL_Query = "SET @row_number = '0'; " _
                & "SELECT hardware_add " _
                & "FROM (" _
                    & "SELECT " _
                    & "@row_number:=@row_number + 1 AS num, " _
                    & "hardware_add AS hardware_add " _
                    & "FROM teller_info" _
                & ") AS sub_query " _
                & "WHERE num = '" & counter & "';"

实际上我是使用heidiSQL直接重新创建该查询并且它工作得很好,所以我有点卡在这里,也许我使用 SET @row_number = '0'; 错了?

摘要:

  1. 我确定它不是MySQL连接问题
  2. 我确信MySQL查询正在运行(通过直接在HeidiSQL上测试)
  3. 获取数据的过程似乎正在起作用,因为我只是复制它从工作函数中获取数据的方式。
  4. 修改:通过关注@ Ken_White评论console.writeline的评论,我能够清楚地看到错误

    这是错误

        MySql.Data.MySqlClient.MySqlException (0x80004005): Fatal error encountered during command execution. ---> MySql.Data.MySqlClient.MySqlException (0x80004005): Parameter '@row_number' must be defined.
       at MySql.Data.MySqlClient.Statement.SerializeParameter(MySqlParameterCollection parameters, MySqlPacket packet, String parmName, Int32 parameterIndex)
       at MySql.Data.MySqlClient.Statement.InternalBindParameters(String sql, MySqlParameterCollection parameters, MySqlPacket packet)
       at MySql.Data.MySqlClient.Statement.BindParameters()
       at MySql.Data.MySqlClient.PreparableStatement.Execute()
       at MySql.Data.MySqlClient.MySqlCommand.ExecuteReader(CommandBehavior behavior)
       at MySql.Data.MySqlClient.MySqlCommand.ExecuteReader(CommandBehavior behavior)
       at MySql.Data.MySqlClient.MySqlCommand.ExecuteReader()
       at TCP_Client.Module_Database.Get_Control_Station_Address(Int32 counter) in C:\Users\xxxxx\xxx.vb:line 198
    

3 个答案:

答案 0 :(得分:5)

似乎添加 Allow User Variables = True到我的数据库连接解决了我的问题。

所以而不是

Public Sub Check_DB_Con()
    'Declare database credentials so we can refresh it in recheck timer
    server = My.Settings.DB_Server
    username = My.Settings.DB_Username
    password = My.Settings.DB_Password
    database = My.Settings.DB_Database

    'Connection String
    MysqlConn.ConnectionString = "server=" & server & ";" _
    & "user id=" & username & ";" _
    & "password=" & password & ";" _
    & "database=" & database 
End Sub

我添加了Allow User Variables=True,因此它将成为

Public Sub Check_DB_Con()
    'Declare database credentials so we can refresh it in recheck timer
    server = My.Settings.DB_Server
    username = My.Settings.DB_Username
    password = My.Settings.DB_Password
    database = My.Settings.DB_Database

    'Connection String
    MysqlConn.ConnectionString = "server=" & server & ";" _
    & "user id=" & username & ";" _
    & "password=" & password & ";" _
    & "database=" & database & ";Allow User Variables=True"
End Sub

答案 1 :(得分:1)

不是在2步SQL中设置rn的值,而是使用交叉连接。我不认为该调用正确处理这两个语句。

 SQL_Query = "SELECT hardware_add " _
            & "FROM (" _
                & "SELECT " _
                & "@row_number:=@row_number + 1 AS num, " _
                & "hardware_add AS hardware_add " _
                & "FROM teller_info" _
                & "CROSS JOIN (SELECT @row_number:=0) AS t " _
            & ") AS sub_query " _
            & "WHERE num = '" & counter & "';"

堆栈示例:ROW_NUMBER() in MySQL

答案 2 :(得分:0)

在连接字符串中添加 ConnectionTimeout 默认命令超时为我解决了此问题。

例如

ConnectionTimeout=300000; default command timeout=300000;