我在此登录表单中遇到错误,该错误是“连接必须有效且开放,有人可以帮助我,这是代码。我无法理解此错误是什么,需要您的帮助
Imports MySql.Data.MySqlClient
Public Class LoginForm1
Dim Username As String
Dim Password As String
Public StudentName As String
'MySql
Dim connection As New MySqlConnection("Server=server;User ID=id;Password=pass;Database=data;SSLMode=None")
Private Sub OK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OK.Click
Username = UsernameTextBox.Text
Password = PasswordTextBox.Text
StudentName = NameTextBox.Text
Dim command As New MySqlCommand("SELECT `Username`, `Password`, `FirstName` FROM `people` WHERE `Username` = @username AND `Password` = @password And `FirstName` = @name", connection)
command.Parameters.Add("@username", MySqlDbType.VarChar).Value = Username
command.Parameters.Add("@password", MySqlDbType.VarChar).Value = Password
command.Parameters.Add("@name", MySqlDbType.VarChar).Value = StudentName
Dim adapter As New MySqlDataAdapter(command)
Dim table As New DataTable()
Dim reader As MySqlDataReader = command.ExecuteReader
adapter.Fill(table)
If table.Rows.Count = 0 Then
MessageBox.Show("Invalid Username Or Password", "Invalid Login", MessageBoxButtons.OK, MessageBoxIcon.Information)
Else
MessageBox.Show("Logged In", "Logged in Successfully", MessageBoxButtons.OK, MessageBoxIcon.Information)
MainMenuForm.Show()
Me.Close()
End If
End Sub
Private Sub Cancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Cancel.Click
Me.Close()
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim webAddress As String = "http://windowsadvancedstudy.com"
Process.Start(webAddress)
End Sub
End Class
答案 0 :(得分:1)
如果您使用的是adapter.Fill
,则无需声明reader。删除此行:
Dim reader As MySqlDataReader = command.ExecuteReader
上面的确切行引发了异常。因为reader
需要打开连接。
并添加以下行:
command.CommandType = CommandType.Text
答案 1 :(得分:0)
在使用前尝试打开连接。
并使用一个“ using”块,以便正确处理它。
此外,最好不要将凭据存储在代码中,而应使用配置文件。
Private Sub OK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OK.Click
Dim connection As MySqlConnection
Using(connection As New MySqlConnection("Server=server;User ID=id;Password=pass;Database=data;SSLMode=None")
connection.Open()
Username = UsernameTextBox.Text
Password = PasswordTextBox.Text
StudentName = NameTextBox.Text
Dim command As New MySqlCommand("SELECT `Username`, `Password`, `FirstName` FROM `people` WHERE `Username` = @username AND `Password` = @password And `FirstName` = @name", connection)
command.Parameters.Add("@username", MySqlDbType.VarChar).Value = Username
command.Parameters.Add("@password", MySqlDbType.VarChar).Value = Password
command.Parameters.Add("@name", MySqlDbType.VarChar).Value = StudentName
Dim adapter As New MySqlDataAdapter(command)
Dim table As New DataTable()
Dim reader As MySqlDataReader = command.ExecuteReader
adapter.Fill(table)
If table.Rows.Count = 0 Then
MessageBox.Show("Invalid Username Or Password", "Invalid Login", MessageBoxButtons.OK, MessageBoxIcon.Information)
Else
MessageBox.Show("Logged In", "Logged in Successfully", MessageBoxButtons.OK, MessageBoxIcon.Information)
MainMenuForm.Show()
Me.Close()
End If
End Using
End Sub