连接必须有效并打开 VB.Net [英] Connection must be valid and open VB.Net
本文介绍了连接必须有效并打开 VB.Net的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想将我的 MySQL 连接到我的 VB.net.我只使用登录表单.我已经给出了代码,但代码给了我这个错误信息:Connection must be valid and open
I want to connect my MySQL to my VB.net.
I am only using Login Form.
I have given the code but the code gives me this error message: 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
谁能帮我解决这个问题?谢谢.
Can anyone help me to fix that? Thanks.
推荐答案
您在任何时候都不会将您的 MysqlConn
或 Query
与您的 Command
相关联> 在尝试对其调用 ExecuteReader
之前.因此,它当时没有有效的连接.
At no point do you associate your MysqlConn
nor Query
to your Command
before trying to call ExecuteReader
on it. As such, it doesn't have a valid connection at that time.
这篇关于连接必须有效并打开 VB.Net的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文