[英]Object reference isn't set to an instance of an object
The error is in this image: 错误在此图像中:
How do I fix this? 我该如何解决?
My code: 我的代码:
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
your sql connection string missing true credentials, this sample to connect to mysql with vb and c#: 您的sql连接字符串缺少真实凭据,此示例使用vb和c#连接到mysql:
https://dev.mysql.com/doc/connector-net/en/connector-net-programming-connecting-open.html https://dev.mysql.com/doc/connector-net/en/connector-net-programming-connecting-open.html
You haven't initialized a new instance of your cmd
variable. 您尚未初始化
cmd
变量的新实例。
Change: 更改:
Dim cmd As MySqlCommand
to: 至:
Dim cmd As New MySqlCommand
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.