[ad_1]
晚上好,伙计们,您正在编写这段代码,但它告诉我我打开了一个 ExecuteReader。
你能帮我一下吗
谢谢
我尝试过的:
VB
Dim namecontr As String Dim rdr3 As Object Dim cmd3 As New MySqlCommand("Select s01_name_contr from s01_tpcontratti where id=" & rdr.Item(2), cnnmysql) rdr3 = cmd3.ExecuteReader rdr3 = cmd3.ToString ' 03/12 ore 17:25 capire perche non mi funziona si blocca 29/12 non ancora sbloccato 'cmd3.Dispose() ' rdr3.Read() If rdr3.HasRows = True Then namecontr = rdr3.Item(0).ToString End If Dim data As Date = CDate(rdr.Item(3).ToString) grid_contratto.Rows.Add(rdr(0).ToString, rdr(2).ToString, namecontr, "stocazzo", data.ToShortDateString) ' rdr3.close() Loop rdr.Close() 'cnnmysql.Clone() End Sub
编辑 – “解决方案 1”中的 OP 代码和已删除的解决方案
VB
Private Sub carica_grigliacontratti(id As String) ' Call connetti_mysql() ' cnnmysql.Open() If CInt("0" & id) = 0 Then Exit Sub Dim sql As String Dim rdr As Object ' Dim rdr2 As Object ' grspecialisti.Rows.Clear() sql = "Select * from a01_contratti where a01_cliente=" & id Dim cmd1 As New MySqlCommand(sql, cnnmysql) rdr = cmd1.ExecuteReader() cmd1.Dispose() If rdr.HasRows = False Then rdr.Close() Exit Sub End If sistema_grcerca() ' Dim perc As Long = 0 ' Dim volte As Integer = 0w cnnmysql.Clone() grid_contratto.Rows.Clear() Do While rdr.Read Dim namecontr As String Dim rdr3 As Object Dim cmd3 As New MySqlCommand("Select s01_name_contr from s01_tpcontratti where id=" & rdr.Item(2), cnnmysql) rdr3 = cmd3.ExecuteReader rdr3 = cmd3.ToString ' 03/12 ore 17:25 capire perche non mi funziona si blocca 29/12 non ancora sbloccato 'cmd3.Dispose() ' rdr3.Read() If rdr3.HasRows = True Then namecontr = rdr3.Item(0).ToString End If Dim data As Date = CDate(rdr.Item(3).ToString) grid_contratto.Rows.Add(rdr(0).ToString, rdr(2).ToString, namecontr, "stocazzo", data.ToShortDateString) ' rdr3.close() Loop rdr.Close() 'cnnmysql.Clone() End Sub
解决方案2
当您调用 ExecuteReader 时,它会“锁定”连接,因为读取器仅在您调用 Read 时才获取一行的信息。 要释放锁,请关闭连接。 处理这个问题的最佳方法是始终在 Using
堵塞[^],然后在该块内的第二个 using 块中创建一个命令,并在其中的第三个块中创建一个读取器:
VB
Using con As New SqlConnection(strConnect) con.Open() Using cmd As New SqlCommand("SELECT iD, description FROM myTable", con) Using reader As SqlDataReader = cmd.ExecuteReader() While reader.Read() Dim myId As Integer = CInt(reader("iD")) Dim myDesc As String = DirectCast(reader("description"), String) Console.WriteLine("ID: {0}" & vbLf & " {1}", myId, myDesc) End While End Using End Using End Using
当您的代码以任何方式退出Using块(例如Break、Return或Exception)时,其中创建的对象将自动关闭并为您处置,并且您永远不会遇到未关闭的读取器或连接的问题。
[ad_2]
コメント