如何解决“关闭阅读器vb.net时无效的阅读尝试”

时间:2019-01-10 09:37:19

标签: vb.net

读取器关闭时尝试读取无效的错误。 vb.net

下面有我用于登录MySQL的代码,但该代码始终显示“关闭阅读器时无效的读取尝试”错误,然后登录。我不明白该代码仍然有什么问题。请协助,这真令人沮丧。谢谢。

 Try
    ConnDB()
    command = con.CreateCommand()
    command.CommandText = "SELECT UserName,Password,Type,EmpNo FROM userstable where UserName=@d1 and Password=@d2 and Active='Yes'"
    command.Parameters.AddWithValue("@d1", txtUsername.Text)
    command.Parameters.AddWithValue("@d2", txtPassword.Text)
    Reader = command.ExecuteReader()
    If Reader.Read = True Then
        ComboBox1.Text = Reader.GetValue(2)
        lblUser.Text = Reader.GetValue(3)
    End If
    If (Reader IsNot Nothing) Then
        Reader.Close()
    End If
    If con.State = ConnectionState.Open Then
        con.Close()
    End If
    If ComboBox1.Text.Length > 0 Then
        MainMenu.lblUserName.Text = Me.txtUsername.Text
        MainMenu.lblType.Text = Me.ComboBox1.Text
        MainMenu.lblOccupation.Text = Me.ComboBox1.Text
        MainMenu.lblUser.Text = Me.lblUser.Text
        Dim st As String = "Successfully logged in"
        LogFunc(txtUsername.Text, st)
        Me.Hide()
        MainMenu.Show()
    Else
        MsgBox("Incorrect username or password..Login is Failed...Try again !", MsgBoxStyle.Critical, "Login")
        txtUsername.SelectAll()
        txtPassword.SelectAll()
        txtUsername.Focus()
        txtPassword.Text = ""
    End If
    command.Dispose()
    ' Reader.Close()
    'command.Dispose()
    con.Close()
Catch ex As Exception
    MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
Finally
    DisconnMy()
End Try

我希望它能成功登录而不显示错误。

对不起,我忘记为连接字符串添加它。

Public Sub ConnDB()
       con.Close()
    Try
        con.ConnectionString = "Server = '" & ServerMySQL & "';  " _
                               & "Port = '" & PortMySQL & "'; " _
                               & "Database = '" & DBNameMySQL & "'; " _
                               & "user id = '" & UserNameMySQL & "'; " _
                               & "password = '" & PwdMySQL & "'"
        con.Open()
    Catch ex As Exception
                MsgBox("The system failed to establish a connection", MsgBoxStyle.Information, "Database Settings")
    End Try
End Sub

1 个答案:

答案 0 :(得分:2)

通常,对所有内容使用“全局”单一连接不是一个好主意。请注意,connection-pooling默认情况下处于启用状态,这意味着.NET将负责物理连接。因此,您应该在使用位置创建,打开,使用和关闭/放置连接。

我很确定这可以解决问题:

Try
    Using con As New MySql.Data.MySqlClient.MySqlConnection(MySettings.Default.SqlConnection)
        Using command = con.CreateCommand()
            command.CommandText = "SELECT UserName,Password,Type,EmpNo FROM userstable where UserName=@d1 and Password=@d2 and Active='Yes'"
            command.Parameters.AddWithValue("@d1", txtUsername.Text)
            command.Parameters.AddWithValue("@d2", txtPassword.Text)
            con.Open()
            Using reader = command.ExecuteReader()
                If reader.Read() Then
                    ComboBox1.Text = reader.GetValue(2)
                    lblUser.Text = reader.GetValue(3)
                End If
                ' NOTE: you dont need to close the reader/command/connection 
                ' if you use the Using-statement, it will use Dispose even in case of an error
            End Using
        End Using
    End Using
Catch ex As Exception
    MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try