已经有一个与此Connection关联的开放DataReader,必须先在vb 2010 professional中关闭

时间:2014-07-23 01:52:08

标签: vb.net

我正在使用vb 2010专业版,我已经“已经有一个与此DataReader相关联的已开放Connection必须首先关闭”问题

这是我的完整源代码:

Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick


    MysqlConn = New MySqlConnection
    MysqlConn.ConnectionString = "server=localhost;userid=root;password=;database=penjara"
    Dim Reader As MySqlDataReader


    MysqlConn.Open()
    Dim query As String
    Dim spath As String
    Dim count As Integer = 0
    Dim mysound As Media.SoundPlayer


    query = "select* from penjara.info"
    Command = New MySqlCommand(query, MysqlConn)
    Reader = Command.ExecuteReader

    While Reader.Read
        count = count + 1

        query = "Select penjara.info where  no =count"
        Command = New MySqlCommand(query, MysqlConn)
        Reader = Command.ExecuteReader

        If (Reader.GetInt32("keadaan") = 1) & (Reader.GetInt32("no") = count) Then
            Form2.Show()
            Me.Hide()
            spath = "C:\Users\User\Music\emergency003.wav"
            mysound = New Media.SoundPlayer(spath)
            mysound.PlayLooping()
        ElseIf (Reader.GetInt32("keadaan") = 1) & (Reader.GetInt32("no") = count) Then
            Form3.Show()
            Me.Hide()
            spath = "C:\Users\User\Music\emergency003.wav"
            mysound = New Media.SoundPlayer(spath)
            mysound.PlayLooping()
        ElseIf (Reader.GetInt32("keadaan") = 1) & (Reader.GetInt32("no") = count) Then
            Form4.Show()
            Me.Hide()
            spath = "C:\Users\User\Music\emergency003.wav"
            mysound = New Media.SoundPlayer(spath)
            mysound.PlayLooping()
        ElseIf (Reader.GetInt32("keadaan") = 1) & (Reader.GetInt32("no") = count) Then
            Form5.Show()
            Me.Hide()
            spath = "C:\Users\User\Music\emergency003.wav"
            mysound = New Media.SoundPlayer(spath)
            mysound.PlayLooping()
        ElseIf (Reader.GetInt32("keadaan") = 1) & (Reader.GetInt32("no") = count) Then
            Form5.Show()
            Me.Hide()
            spath = "C:\Users\User\Music\emergency003.wav"
            mysound = New Media.SoundPlayer(spath)
            mysound.PlayLooping()
        End If
    End While
End Sub

2 个答案:

答案 0 :(得分:3)

您正在阅读Reader,然后尝试在其上打开一个新的阅读器

While Reader.Read              <<< looping on this
    count = count + 1

    query = "Select penjara.info where  no =count"
    Command =  New MySqlCommand(query, MysqlConn)      <<<<<<<<<<<<< trying to open new one

    Reader = cmd.ExecuteReader                    <<<<<<<<<<<<< trying to open new one

您应该在循环中创建新的连接和读者(不重用变量)。它们应该像这样替换:

While Reader.Read              <<< looping on this
    count = count + 1

    Using con as New MySqlConnection(MysqlConn.ConnectionString)
    Using cmd as New MySqlCommand(query, con)  
    Using rdr = cmd.ExecuteReader         

并且您的IF语句应该使用rdr:

If (rdr.GetInt32("keadaan") .... etc

并在循环的底部

End Using
End Using
End Using

因为这会关闭打开的连接。

此外,查询看起来很可疑你确定你不想要像......那样的东西。

query = String.Format("Select penjara.info where  no ={0}", count)

答案 1 :(得分:0)

显示错误是因为尝试在下一个计时器滴答中执行代码时连接已打开。因此可以通过此代码打开连接来避免它

  If MysqlConn.State = ConnectionState.Open Then
    MysqlConn.Close()
  else
    MysqlConn.Open()
  End If