我有這樣的代碼:「命令執行過程中遇到致命錯誤」 我使用SET和SELECT XXX FROM(SELECT XXX FROM XXX)
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:[email protected]_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:[email protected]_number + 1 AS num, " _
& "hardware_add AS hardware_add " _
& "FROM teller_info" _
& ") AS sub_query " _
& "WHERE num = '" & counter & "';"
其實我重新創建直接使用heidiSQL該查詢,它工作的偉大,讓我有點困在這裏,也許我使用SET @row_number = '0';
錯了嗎?
總結:
- 我敢肯定,這不是MySQL連接問題
- 我相信,MySQL的查詢工作(通過測試它直接在HeidiSQL)
- 過程獲取數據似乎工作,因爲我只是複製它從一個工作函數獲取數據的方式。
編輯:遵循@評論console.writeline
的Ken_White的評論我能夠清楚地看到錯誤
這裏的錯誤
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
你爲什麼引用你的'WHERE'子句中'num'? 'num'是一個數字,所以你不應該用引號包圍它。 (你在這兩個SQL語句中都有同樣的問題,所以我懷疑第二個是否正在工作。)將查詢的最後部分更改爲'&「WHERE num =」&counter & ";「',看看是否修復它。它不會,嘗試打開配置文件中的詳細異常並運行以查看實際發生的異常。然後**立即**開始對使用參數化查詢進行一些研究,以便停止連接SQL語句(搜索* SQL注入*)。 –
@KenWhite,是的。我只是試過,如果它能工作,但最初我沒有包括那些。我現在刪除它們,但仍然無法使用。你能幫我解釋一下如何打開詳細的例外情況嗎?感謝您建議參數化查詢。 –
首先在'catch'子句中註釋掉你的'Console.WriteLine',然後讓異常被引發。你基本上只會寫出一小部分有用的信息。然後搜索此網站* [vb.net]詳細的例外*。 –