其他SqlDataReader需要关闭,虽然我已经关闭了其他任何其他

时间:2017-04-13 13:11:17

标签: sql-server vb.net

我已经关闭了所有读者了吗?每次触发该功能时,都会导致错误

  

System.InvalidOperationException:'读取器关闭时无效尝试调用Read。

现在我真的很沮丧。

我的代码出了什么问题?一切似乎都很好。

Private Sub Button_EditData_Click(sender As Object, e As EventArgs) Handles Button_EditData.Click
    FormEnabler()
    Me.TextBox_BranchID.Enabled = False
    Me.Button_AddNew.Enabled = False
    Me.Button_EditData.Enabled = False
    Me.Button_DeleteData.Enabled = False
    Me.Button_Save.Enabled = True
    Me.Button_Cancel.Enabled = True
    Me.Button_ManageThisBranchStock.Enabled = False
    Me.Button_ManageThisBranchEmployee.Enabled = False

    theConnection.Open()

    Dim theEditInputCommand As New SqlCommand
    Dim theEditInputDataReader As SqlDataReader

    theEditInputCommand.Connection = theConnection
    theEditInputCommand.CommandText = "SELECT * FROM Branch WHERE BranchID = '" & Me.TextBox_BranchID.Text & "'"
    theEditInputDataReader = theEditInputCommand.ExecuteReader()

    If theEditInputDataReader.Read() Then
        Me.TextBox_Title.Text = theEditInputDataReader.Item("Title")
        Me.RichTextBox_Address.Text = theEditInputDataReader.Item("Address")
        Me.TextBox_ContactNumber.Text = theEditInputDataReader.Item("ContactNo")
        Me.ComboBox_BranchManager.Text = theEditInputDataReader.Item("BranchManager")
        theEditInputDataReader.Close()
    End If

    theConnection.Close()

    theConnection.Open()

    Dim theEditInputBranchManagerCommand As New SqlCommand
    Dim theEditInputBranchManagerDataReader As SqlDataReader
    Dim theEditInputBranchManagerDataTable As New DataTable

    theEditInputBranchManagerCommand.Connection = theConnection
    theEditInputBranchManagerCommand.CommandText = "SELECT EmployeeID FROM AssignmentDetail WHERE BranchID = '" & Me.TextBox_BranchID.Text & "'"
    theEditInputBranchManagerDataReader = theEditInputBranchManagerCommand.ExecuteReader()
    theEditInputBranchManagerDataTable.Load(theEditInputBranchManagerDataReader)

    If theEditInputBranchManagerDataReader.Read() Then
        Me.ComboBox_BranchManager.ValueMember = "EmployeeID"
        Me.ComboBox_BranchManager.DisplayMember = "EmployeeID"
        Me.ComboBox_BranchManager.DataSource = theEditInputBranchManagerDataTable
        theEditInputBranchManagerDataReader.Close()
    Else
        Me.ComboBox_BranchManager.ValueMember = "'-'"
        theEditInputBranchManagerDataReader.Close()
    End If

    theConnection.Close()
End Sub

调试器指出了这里的错误:

If theEditInputBranchManagerDataReader.Read() Then
    Me.ComboBox_BranchManager.ValueMember = "EmployeeID"
    Me.ComboBox_BranchManager.DisplayMember = "EmployeeID"
    Me.ComboBox_BranchManager.DataSource = theEditInputBranchManagerDataTable
    theEditInputBranchManagerDataReader.Close()
Else
    Me.ComboBox_BranchManager.ValueMember = "'-'"
    theEditInputBranchManagerDataReader.Close()
End If

2 个答案:

答案 0 :(得分:3)

对于具有 IDisposable 的所有内容,您应该实施使用

Using reader As SqlDataReader = command.ExecuteReader()
    While reader.Read()
        FPath = reader(0)
    End While
End Using

如果您不想使用 USING 最终使用SQL,请确保丢弃阅读器 .Dispose()好用:

SqlConnection.ClearPool(con)

答案 1 :(得分:0)

好的,现在我确定问题出在Load(DataTable)

Welp,我已经改变主意并将其切换为DataSet而不是DataTable

感谢您回答我的问题。祝你有个美好的一天。

哦,这是工作代码

Using theEditCheckBranchManagerDataReader As SqlDataReader = theEditCheckBranchManagerCommand.ExecuteReader()
        If theEditCheckBranchManagerDataReader.Read() Then
            theEditCheckBranchManagerCommand.Dispose()
            theEditCheckBranchManagerDataReader.Close()
            theEditCheckBranchManagerDataReader.Dispose()
            theConnection.Close()
            SqlConnection.ClearPool(theConnection)

            Dim theEditInputBranchManagerCommand As New SqlCommand
            Dim theEditInputBranchManagerDataSet As New DataSet
            Dim theEditInputBranchManagerDataAdapter As New SqlDataAdapter

            theEditInputBranchManagerCommand.Connection = theConnection
            theEditInputBranchManagerCommand.CommandText = "SELECT EmployeeID FROM AssignmentDetail WHERE BranchID = '" & Me.TextBox_BranchID.Text & "'"
            theEditInputBranchManagerDataAdapter.SelectCommand = theEditInputBranchManagerCommand

            theEditInputBranchManagerDataAdapter.Fill(theEditInputBranchManagerDataSet)
            theEditInputBranchManagerDataAdapter.Dispose()
            theEditInputBranchManagerCommand.Dispose()

            Me.ComboBox_BranchManager.DataSource = theEditInputBranchManagerDataSet.Tables(0)
            Me.ComboBox_BranchManager.ValueMember = "EmployeeID"
            Me.ComboBox_BranchManager.DisplayMember = "EmployeeID"
        Else
            theEditCheckBranchManagerCommand.Dispose()
            theEditCheckBranchManagerDataReader.Close()
            theEditCheckBranchManagerDataReader.Dispose()
            theConnection.Close()
            SqlConnection.ClearPool(theConnection)

            Me.ComboBox_BranchManager.DataSource = Nothing
            Me.ComboBox_BranchManager.Items.Clear()
            Me.ComboBox_BranchManager.Text = "-"
        End If
    End Using