我正在使用VB2010 Express,我有一个带有以下字段Ext,CostAssing和CostAct的Mysql平板电脑,我正在尝试从第一个查询中获得的结果更新Mysql表:
**
Imports MySql.Data.MySqlClient
Imports System
Imports System.IO
Public Class Form1
Public dbconn As New MySqlConnection
Public sql As String
Public sqlQuery As String
Public SQLcmd As MySqlCommand
Public dbcomm As MySqlCommand
Public dbread As MySqlDataReader
Dim Ext As String
Dim CostAssing As Integer
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
dbconn = New MySqlConnection("Data Source=localhost ; user id=root ; password=password ; database=calls")
'First Query Get Extension and CostAssing
Try
dbconn.Open()
sqlQuery = "SELECT Ext,CostAssing FROM estruc "
SQLcmd = New MySqlCommand(sqlQuery, dbconn)
dbread = SQLcmd.ExecuteReader
While dbread.Read()
Ext = dbread.Item("Ext")
CostAssing = dbread.Item("CostAssing")
MsgBox("Ext:" & Ext)
'Second Query Update Ext from CostAct to CostAssing
Try
sqlQuery = "UPDATE estruc SET CostAct = '" & "1000" & "' WHERE Ext = '" & Ext & "'"
SQLcmd = New MySqlCommand(sqlQuery, dbconn)
dbread = SQLcmd.ExecuteReader
Catch ex As Exception
MsgBox("Error 2 is :" & ex.Message)
End Try
End While
Catch ex As Exception
MsgBox("Error 1 is :" & ex.Message)
End Try
dbread.Close()
End Sub
End Class
**
第一个查询运行正常,我得到字段Ext和CostAssing,但是当第二个Query尝试更新de字段CostAct时,我得到以下错误(由Catch ex As Exception MsgBox报告(“错误2是:”& ex。消息)):
“错误2是:已经有一个与此Connection关联的打开的Datareader,必须先关闭它。”
PLease,有什么想法吗?
答案 0 :(得分:0)
在第一次查询后添加以下行以关闭datareader
dbread.Close();
您希望先存储dbread中的值(数据表或列表等),关闭while循环,然后运行第二个查询:
SQLcmd.ExecuteNonQuery();