“如何修复”;当读卡器关闭时尝试读取无效vb.net“;
读卡器关闭时尝试读取无效错误。vb.net 下面是我用来登录MySQL的代码,但它一直显示错误“读卡器关闭时尝试读取无效”,然后登录。我不明白代码还有什么问题。请帮忙,这太令人沮丧了。谢谢“如何修复”;当读卡器关闭时尝试读取无效vb.net“;,vb.net,Vb.net,读卡器关闭时尝试读取无效错误。vb.net 下面是我用来登录MySQL的代码,但它一直显示错误“读卡器关闭时尝试读取无效”,然后登录。我不明白代码还有什么问题。请帮忙,这太令人沮丧了。谢谢 Try ConnDB() command = con.CreateCommand() command.CommandText = "SELECT UserName,Password,Type,EmpNo FROM userstable where UserName=@d1 and P
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
一般来说,使用“全局”单一连接不是一个好主意。请注意,默认情况下是启用的,这意味着.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
一般来说,使用“全局”单一连接不是一个好主意。请注意,默认情况下是启用的,这意味着.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
在哪里打开连接?我的模块中有一个名为ConnDB()的公共子模块,为什么不显示它?一般来说,使用“全局”单一连接不是一个好主意。请注意,默认情况下启用了连接池,这意味着.NET将负责物理连接。因此,您应该在使用连接的地方创建、打开、使用和关闭/释放连接。我刚才已将其添加到我的问题中public Sub disconmy()con.close()con.dispose()command.dispose()命令End Sub.每次打开连接时,我都会用它来关闭所有连接。您在哪里打开连接?我的模块中有一个名为ConnDB()的公共Sub。为什么不显示它?一般来说,使用“全局”单一连接不是一个好主意。请注意,默认情况下启用了连接池,这意味着.NET将负责物理连接。因此,您应该在使用连接的地方创建、打开、使用和关闭/释放连接。我刚才已将其添加到我的问题中public Sub disconmy()con.close()con.dispose()command.dispose()命令End Sub.每次打开它时,我都会用它来关闭所有连接。它会显示这样一个通知:“未声明MySettings,由于其保护级别,它可能无法访问”。@NickOsoo:是的,当然可以,因为您没有在设置文件中存储连接字符串(建议使用配置文件)。我只是不想复制你的连接字符串。您可以提供一个方法
GetConnectionString
,并调用该方法。。我的app.config中有上述连接字符串,我试图将MySettings.Default.SqlConnection更改为oppsdatabaseConnectionString,但仍然显示我无法连接到任何指定的MySQL主机。它显示此通知“未声明MySettings,由于其保护级别,它可能无法访问”。@NickOsoo:是的,当然,因为您没有将连接字符串存储在设置文件中(建议使用配置文件)。我只是不想复制你的连接字符串。您可以提供一个方法GetConnectionString
,并调用该方法。。我的app.config中有上述连接字符串,我尝试将MySettings.Default.SqlConnection更改为oppsdatabaseConnectionString,但仍然显示我无法连接到任何指定的MySQL主机