错误出现在此图片中:
我该如何解决这个问题?
我的代码:
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
Dim con As New MySqlConnection("host=localhost; username=root; database= login")
Dim cmd As MySqlCommand
Dim dr As MySqlDataReader
con.Open()
cmd.Connection = con 'this is where I have an error
cmd.CommandText = "select userID, password from login where userID= ' " & userIDtxt.Text & " ' and password = ' " & pwTxt.Text & " '"
dr = cmd.ExecuteReader
If dr.HasRows Then
MsgBox("Login successful!")
Me.Hide()
MainForm.Show()
Else
MsgBox("Username or password does not match")
userIDtxt.Text = ""
pwTxt.Text = " "
End If
End Sub
End Class
答案 0 :(得分:1)
你的sql连接字符串缺少真正的凭据,这个示例用vb和c#连接到mysql:
https://dev.mysql.com/doc/connector-net/en/connector-net-programming-connecting-open.html
答案 1 :(得分:0)
您尚未初始化cmd
变量的新实例。
变化:
Dim cmd As MySqlCommand
为:
Dim cmd As New MySqlCommand