我有这段代码:
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';
错了?
摘要:
修改:通过关注@ 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
答案 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 & "';"
答案 2 :(得分:0)
在连接字符串中添加 ConnectionTimeout 和默认命令超时为我解决了此问题。
例如
ConnectionTimeout=300000; default command timeout=300000;