我一直在使用 vb.net (Visual Studio 2022) 在 MySQL 服务器上运行 INSERT 和 DELETE 查询,但现在我需要运行 SELECT 查询并获取返回值。
我运行 INSERT 查询的代码如下所示:
Public Function StoreData() As Object
'Write the times and types to the tblparams table of the database
Dim strS1Type As String, strS2Type As String, sM1Ts As String, sM2Ts As String, sM1Tr As String, sM2Tr As String
Dim connectionString As String, iReturn As Boolean
'Connection String to connect with MySQL database.
connectionString = "server=xx.xx.xx.xx;userid=user;password=password;database=database"
Using SQLConnection As New MySqlConnection(connectionString)
Using sqlCommand As New MySqlCommand()
With sqlCommand
.CommandText = "INSERT INTO tbldata(M1Type, M1Start, M1Tr, M2Type, M2Start, M2Tr, M1SheetType, M2SheetType) VALUES(@M1Type, @M1Start, @M1Tr, @M2Type, @M2Start, @M2Tr, @M1SheetType, @M2SheetType);"
.Connection = SQLConnection
.CommandType = CommandType.Text
.Parameters.AddWithValue("@M1Type", strS1Type)
.Parameters.AddWithValue("@M1Start", sM1Ts)
.Parameters.AddWithValue("@M1Tr", sM1Tr)
.Parameters.AddWithValue("@M2Type", strS2Type)
.Parameters.AddWithValue("@M2Start", sM2Ts)
.Parameters.AddWithValue("@M2Tr", sM2Tr)
.Parameters.AddWithValue("@M1SheetType", GlobalVariables.strM1SheetType)
.Parameters.AddWithValue("@M2SheetType", GlobalVariables.strM2SheetType)
End With
Try
SQLConnection.Open()
sqlCommand.ExecuteNonQuery()
iReturn = True
Catch ex As MySqlException
MsgBox(ex.Message.ToString)
iReturn = False
Finally
SQLConnection.Close()
End Try
End Using
End Using
End Function
根据我所读到的内容,将代码放在 USING 子句中可确保在退出 USING 子句时关闭与数据库的连接。我尝试修改此代码以从 SELECT 查询返回数据:
Public Sub RestoreData()
'Read the start times and types from the tblparams table of the mtmdev database and load into controls
Dim connectionString As String, iReturn As Boolean, vResult As String, i As Integer, a As Array
'Define connection String to connect with MySQL database.
connectionString = "server=xx.xx.xx.xx;userid=user;password=password;database=database"
Using SQLConnection As New MySqlConnection(connectionString)
Using sqlCommand As New MySqlCommand()
With sqlCommand
.CommandText = "SELECT * FROM tbldata;"
.Connection = SQLConnection
.CommandType = CommandType.Text
End With
i = 0
Try
SQLConnection.Open()
sqlCommand.ExecuteReader()
While vResult = sqlCommand.ExecuteReader.GetValue(i)
MsgBox("Result is:" & vbCrLf & vResult)
i = i + 1
iReturn = True
End While
Catch ex As MySqlExceptionSystem
MsgBox(ex.Message.ToString)
iReturn = False
Finally
SQLConnection.Close()
End Try
End Using
End Using
End Sub
但这会导致以下错误: System.InvalidOperationException:'此 MySqlConnection 已在使用中。请参阅https://fl.vu/mysql-conn-reuse'
当到达终点线时:
While vResult = sqlCommand.ExecuteReader.GetValue(i)
我应该使用不同的语法来读取查询返回的字段值吗?
您拨打了两次
ExecuteReader
,这是没有意义的。正如网络上的任何示例所示,您调用 ExecuteReader
并且它返回一个数据读取器,然后您使用它来读取数据,例如
Using connection As New MySqlConnection(connectionString),
command As New MySqlCommand(query, connection)
connection.Open()
Using reader = command.ExecuteReader()
While reader.Read()
Console.WriteLine($"{reader.GetString(reader.GetOrdinal("Id"))} - {reader.GetString(reader.GetOrdinal("Name"))}")
End While
End Using
End Using
当且仅当有一行要读取时,Read
才会返回True
,这样循环就会读取结果集中的每一行。