我有一个项目需要在读取时更新数据库DataReader
,它说DataReader
已经在这个连接上打开了,必须先关闭。
这是我的代码:
Try
conn = fOpenConnection()
cmd = conn.CreateCommand
queryjadi = "select *..."
cmd = New MySqlCommand(queryjadi, conn)
dbReader = cmd.ExecuteReader
While dbReader.Read
For Each lvi In lsvBOM.Items
If dbReader("kode") = lvi.Text.ToString Then
**'here i want to update my database**
End If
Next
End While
dbReader.Close()
queryjadi = "SELECT * ... "
cmd = New MySqlCommand(queryjadi, conn)
dbReader = cmd.ExecuteReader
For Each lvi In lsvBOM.Items
status = False
While dbReader.Read
If dbReader("kode") = lvi.Text Then
status = True
End If
End While
If status = False Then
'untuk insert
**'here i want to insert into database**
End If
Next
MessageBox.Show("Data sudah disimpan", "Informasi", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1)
Catch sqlEx As MySqlException
fSQLError(sqlEx.Number, sqlEx.Message)
Catch ex As Exception
MessageBox.Show(ex.Message, "Kesalahan", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1)
Finally
dbReader.Close()
cmd.Dispose()
conn.Close()
End Try
我知道我在读取时无法访问数据库DataReader
,如何解决?