错误已经有一个与此连接相关联的打开的datareader,必须先关闭它vb.net“rd=cmd.executereader”

nc1teljy  于 2021-06-18  发布在  Mysql
关注(0)|答案(1)|浏览(365)
Sub CariData()
    CMD = New MySqlCommand("Select * from tbanggota where nomoragt='" & ComboBox1.Text & "'", conn)
    RD = CMD.ExecuteReader
    RD.Read()
    If RD.HasRows Then
        LblNamaAgt.Text = RD.Item("namaagt")
        CMD = New MySqlCommand("Select SUM(totalpjm) AS KETEMU FROM tbpinjam WHERE Nomoragt='" & ComboBox1.Text & "'", conn)
        RD = CMD.ExecuteReader
        RD.Read()
        LblTelahPjm.Text = RD.Item(0)
        If LblTelahPjm.Text = 0 Then
            MsgBox("" & LblNamaAgt.Text & " tidak punya pinjaman")
        End If
    Else
        MsgBox("Nomor anggota tidak teRDaftar")
        ComboBox1.Focus()
        Exit Sub
    End If
    Call Pinjaman()
End Sub
h79rfbju

h79rfbju1#

标题中提供的错误是很有解释性的。
您正在共享 conn 两个命令之间的变量。如果您的查询是连续的并且在它们之间是关闭的,那么这是很好的。在您的情况下,您应该创建两个不同的连接并将它们传递给您的命令。我不知道您的整个应用程序,但一般来说,在尽可能小的范围内创建/处理您的连接和命令是一种很好的做法,不要让它们长时间存在。

Sub CariData()
    Using conn As New SqlConnection("connection string here")
        conn.Open()
        Using CMD As New SqlCommand("Select * from tbanggota where nomoragt='" & Combobox1.Text & "'", conn)
            Using RD As SqlDataReader = CMD.ExecuteReader
                RD.Read()
                If RD.HasRows Then
                    LblNamaAgt.Text = RD.Item("namaagt")
                    Using innerconn As New SqlConnection("connection string here")
                        innerconn.Open()
                        Using innerCMD As New SqlCommand("Select SUM(totalpjm) AS KETEMU FROM tbpinjam WHERE Nomoragt='" & Combobox1.Text & "'", innerconn)
                            Using innerRD As SqlDataReader = innerCMD.ExecuteReader
                                innerRD.Read()
                                LblTelahPjm.Text = innerRD.Item(0)
                                If LblTelahPjm.Text = 0 Then
                                    MsgBox("" & LblNamaAgt.Text & " tidak punya pinjaman")
                                Else
                                    MsgBox("Nomor anggota tidak teRDaftar")
                                    Combobox1.Focus()
                                    Exit Sub
                                End If
                            End Using
                        End Using
                    End Using
                End If
                Call Pinjaman()
            End Using
        End Using
    End Using
End Sub

此外,您的查询似乎彼此不依赖,如果按以下方式编写,则可以使用相同的连接顺序运行:

Sub CariData()
    Using conn As New SqlConnection("connection string here")
        conn.Open()
        Using CMD As New SqlCommand("Select * from tbanggota where nomoragt='" & Combobox1.Text & "'", conn)
            Using RD As SqlDataReader = CMD.ExecuteReader
                RD.Read()
                If RD.HasRows Then
                    LblNamaAgt.Text = RD.Item("namaagt")
                End If
            End Using
        End Using
        Using CMD As New SqlCommand("Select SUM(totalpjm) AS KETEMU FROM tbpinjam WHERE Nomoragt='" & Combobox1.Text & "'", conn)
            Using RD As SqlDataReader = CMD.ExecuteReader
                RD.Read()
                LblTelahPjm.Text = RD.Item(0)
                If LblTelahPjm.Text = 0 Then
                    MsgBox("" & LblNamaAgt.Text & " tidak punya pinjaman")
                Else
                    MsgBox("Nomor anggota tidak teRDaftar")
                    Combobox1.Focus()
                    Exit Sub
                End If
            End Using
        End Using
        Call Pinjaman()
    End Using
End Sub

相关问题