我想将我的MySQL连接到我的VB.net。
我只使用登录表单。
我已经给出了代码,但代码给了我这个错误消息:Connection must be valid and open
这是我的代码:
Imports MySql.Data.MySqlClient
Public Class Login
Dim MysqlConn As MySqlConnection
Dim Command As MySqlCommand
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
MysqlConn = New MySqlConnection
MysqlConn.ConnectionString ="server=db4free.net;port=3306;userid=exd****;password=****;database=exd****"
Dim Reader As MySqlDataReader
Try
MysqlConn.Open()
Dim Query As String
Query = "select * from member where Username='" & UsernameTxt.Text & "' and Password='" & PasswordTxt.Text & "' "
Command = New MySqlCommand
Reader = Command.ExecuteReader
Dim count As Integer
count = 0
While Reader.Read
count = count + 1
End While
If count = 1 Then
MessageBox.Show("Correct !")
ElseIf count > 1 Then
MessageBox.Show("Duplicate !")
Else
MessageBox.Show("Not Correct !")
End If
MysqlConn.Close()
Catch ex As Exception
MsgBox(ex.Message)
Finally
MysqlConn.Dispose()
End Try
End Sub
End Class
任何人都可以帮我解决这个问题吗?感谢。
答案 0 :(得分:3)
要将Query
和Command
与您需要的连接相关联:
Command = New MySqlCommand(Query, MysqlConn)
然后,您可以执行操作以检索所需的数据。
答案 1 :(得分:2)
在尝试呼叫MysqlConn
之前,您没有将Query
和Command
与ExecuteReader
相关联。因此,它当时没有有效的连接。
答案 2 :(得分:0)
Query =“select * from member where Username ='”& UsernameTxt.Text& “'和密码='”& PasswordTxt.Text& “'”, nombredelaconexion