在VB中使用线程从MySQL数据库获取数据,net

getting data from mysql database by using thread in vb.,net

提问人:M.J 提问时间:12/14/2022 最后编辑:ShadowM.J 更新时间:12/14/2022 访问量:71

问:

我正在尝试从数据库中获取数据,但出现错误:

已存在与此连接关联的打开的 DataReader 必须首先关闭

我所做的是以下代码:

1:我有一个包含以下子的模块:

Public Function CheckServerConn() As Boolean
        Try
            _ServerConnStr = New MySqlConnection(My.Settings.DbPath)
            If _ServerConnStr.State = ConnectionState.Open Then
                _ServerConnStr.Close()
            End If

            If _ServerConnStr.State = ConnectionState.Closed Then
                _ServerConnStr.Open()
                Return True
            End If

        Catch ex As Exception
            MsgBox("Check The Conn " & ex.Message, Me_MsgInfoStyle, Me_MsgCaptionStr)
            Return False
        End Try
#Disable Warning BC42353 ' Function doesn't return a value on all code paths
    End Function

2:我在类中有一个名为“ClsMySql”的子例程:

'GetData
    Public Sub GetData(ByVal SqlStr As String, ByVal xDt As DataTable, ByVal xPar() As MySqlParameter)
        Using xCmd As New MySqlCommand() With {
                    .CommandType = CommandType.Text,
                    .CommandText = SqlStr,
                    .Connection = _ServerConnStr,
                    .CommandTimeout = 5000000
                    }

            If xPar IsNot Nothing Then
                For i As Integer = 0 To xPar.Length - 1
                    xCmd.Parameters.Add(xPar(i))
                Next
            End If

            Using xDa As New MySqlDataAdapter(xCmd)
                xDa.Fill(xDt)
            End Using
            xDt.Dispose()
        End Using
    End Sub

3:我有一个表的类,其方法如下:

Public Sub MySql_Get_Daf()
    xClsMySql = New ClsMySql
    Dim SqlStr As String = "SELECT RegID,RegType, tblregs1.`RegRef`,`RegDate`, COUNT(`RegdID`) AS xCount 
                        ,IF(COUNT(`RegdID`) =3,'Ok','Error') AS xResult FROM tblregs1
                        INNER JOIN tblregs2 ON tblregs2.RegRef = tblregs1.RegRef
                        WHERE `RegType` = 'Daf'
                        GROUP BY tblregs1.`RegRef`  
                        ORDER BY COUNT(`RegdID`)  ASC"
    Dt_Get_Daf = New DataTable()
    xClsMySql.GetData(SqlStr, Dt_Get_Daf, Nothing)

End Sub

Public Sub MySql_Get_Qbd()
    xClsMySql = New ClsMySql
    Dim SqlStr As String = "SELECT RegID,RegType, tblregs1.`RegRef`,`RegDate`, COUNT(`RegdID`) AS xCount 
                        ,IF(COUNT(`RegdID`) =3,'Ok','Error') AS xResult FROM tblregs1
                        INNER JOIN tblregs2 ON tblregs2.RegRef = tblregs1.RegRef
                        WHERE `RegType` = 'Qbd'
                        GROUP BY tblregs1.`RegRef`  
                        ORDER BY COUNT(`RegdID`)  ASC"
    Dt_Get_Qbd = New DataTable()
    xClsMySql.GetData(SqlStr, Dt_Get_Qbd, Nothing)
End Sub

Public Sub MySql_Get_All()
    Dim xThread As Thread = New Thread(Sub() MySql_Get_Daf())
    Dim xThread2 As Thread = New Thread(Sub() MySql_Get_Qbd())

    xThread.Start()
    xThread2.Start()

End Sub

当我通过按钮调用MySql_Get_All时,它会给我下一个错误:

已存在与此连接关联的打开的 DataReader 必须首先关闭

谁能告诉我这里出了什么问题

vb.net 多线程 mysql-connector

评论

3赞 jmcilhinney 12/14/2022
不要使用通用连接。创建命令时,请为其创建新连接。
0赞 M.J 12/15/2022
@jmcilhinney好的,但是你能告诉我如何检查一次连接,然后按照你说的去做吗?
0赞 Hursey 12/15/2022
根据@jmcilhinney评论,只是不要使用通用连接。这样你就不需要担心检查一次连接(无论这实际上意味着什么)。当且仅在需要时使用 Using 块定义连接。关于这个主题有数以千计的实例和教程
0赞 Tu deschizi eu inchid 12/15/2022
以下是 C# 语言,但在 VB.NET 中非常相似:stackoverflow.com/a/74576869/10024425stackoverflow.com/a/71540398/10024425stackoverflow.com/a/66987061/10024425

答: 暂无答案