Mysql VB.net中的远程数据库连接不工作
我目前正在VB.net应用程序中使用MySQL连接。我有一个表格,其代码如下:Mysql VB.net中的远程数据库连接不工作,mysql,vb.net,connection-string,Mysql,Vb.net,Connection String,我目前正在VB.net应用程序中使用MySQL连接。我有一个表格,其代码如下: Imports System.Data Imports System.Data.SqlClient Public Class Form4 Private Sub Form4_Load(sender As Object, e As EventArgs) Handles MyBase.Load End Sub Private Sub Button2_Click(ByVal sender As System.
Imports System.Data
Imports System.Data.SqlClient
Public Class Form4
Private Sub Form4_Load(sender As Object, e As EventArgs) Handles MyBase.Load
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
ConnectToSQL()
End Sub
Private Sub ConnectToSQL()
Dim con As New SqlConnection
Dim cmd As New SqlCommand
Dim Password As String
Dim Password2 As String
Dim userName As String
Try
If con.ConnectionString = "Network Library=DBMSSOCN;""Data Source=myserver,1433;""Initial Catalog=users;""User ID=myuser;password=mypass;" Then
con.Open()
cmd.Connection = con
cmd.CommandText = "SELECT user_username, user_pass FROM users WHERE (user_username = '" & txtUsername.Text & "' ) AND (user_pass = '" & txtPassword.Text & "')"
Dim lrd As SqlDataReader = cmd.ExecuteReader()
If lrd.HasRows Then
While lrd.Read()
Password = lrd("Password").ToString()
userName = lrd("UserName").ToString()
Password2 = txtPassword.Text()
If Password = Password2 And userName = txtUsername.Text Then
MessageBox.Show("Logged in successfully as " & userName, "", MessageBoxButtons.OK, MessageBoxIcon.Information
)
Form2.Show()
Me.Hide()
txtPassword.Text = ""
txtUsername.Text = ""
End If
End While
Else
MessageBox.Show("Username or Password incorrect...", "Authentication Failed", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
txtPassword.Text = ""
txtUsername.Text = ""
End If
End If
Catch ex As Exception
MessageBox.Show("Error while connecting to SQL Server." & ex.Message)
Finally
con.Close()
End Try
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Me.Close()
End Sub
End Class
每次运行应用程序时,我都会正确输入登录详细信息,然后单击登录按钮(按钮2)。问题是,它没有任何作用。据我所知,它不会抛出异常,甚至不会尝试登录到服务器。我用自己服务器的登录详细信息替换了登录详细信息,所以这不是问题所在。我错过什么了吗?不要以明文形式存储密码!
此外,您的代码容易出错 不会发生任何事情,因为此
If
永远不会为真:
...
If con.ConnectionString = "Network Library=DBMSSOCN;""Data Source=myserver,1433;""Initial Catalog=users;""User ID=myuser;password=mypass;" Then
...
我懂了。。。我已经删除了
If
语句,但是现在我得到了一个错误:“连接到SQL Server时出错。关键字不受支持:'data source'”。连接字符串似乎乱七八糟。对于mysql,它应该类似于Server=myServerAddress;数据库=myDataBase;Uid=我的用户名;Pwd=我的密码代码>。此外,加倍的双qoutes也不是必需的/错误的-现在看看是否有一个奇怪的异常。。。“抛出异常:System.Data.dll中的:‘System.Data.SqlClient.SqlException’”您标记了mysql,但尝试使用MS SQL Server使用的System.Data.SqlClient
。这并不是例外的全部内容。编辑您的问题并添加新的connectionstring和整个异常。您需要使用MySqlClient
。。。