我正在尝试连接到数据库,但它不起作用......
代码:
Dim conn As MysqlConnection
conn = New Mysqlconnection()
conn.ConnectionString = "server=http://www.*****.net/phpMyAdmin; user id=*****; password=****; database=login"
Try
conn.Open()
Catch myerror As MySqlException
MsgBox("Error connecting to database!")
End Try
它总是说错误连接到数据库!
有什么问题?
答案 0 :(得分:3)
您无法使用http://www.*****.net/phpMyAdmin
作为服务器名称。它应该只是您的域名(主机名)mydomain.com
或IP地址192.168.0.10
它应如下所示:
Dim conn As MySqlConnection = New MySqlConnection
conn.ConnectionString = "Host=192.168.0.10;user=root;password=root"
conn.Open()
答案 1 :(得分:2)
首先确保安装了MySQL Connector / NET。检查所有凭据是否正确。然后试试这个片段。
Imports MySql.Data.MySqlClient
Public Class MySQLConnect
Private db_con As New MySqlConnection
Private Sub connect()
Dim dbname As String = "DBNAME"
Dim dbhost As String = "localhost"
Dim user As String = "root"
Dim pass As String = "DBPASSWORD"
If Not db_con Is Nothing Then db_con.Close()
db_con.ConnectionString = String.Format("server={0}; user id={1}; password={2}; database={3}; pooling=false", dbhost, user, pass, dbname)
Try
db_con.Open()
Catch ex As MySqlException
MsgBox("Database Error:[" & ex.Message & "]")
End Try
End Sub
End Class
试试this link,这非常直接。
答案 2 :(得分:-2)
Imports System.Data.SqlClient
Imports MySql.Data.MySqlClient
Public Class LoginForm1
Dim mysqlconnection As MySqlConnection
Private Sub OK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OK.Click
mysqlconnection = New MySqlConnection
mysqlconnection.ConnectionString = "server=servername.com;port=portnum;user id=username ;pwd=password;database=databasename"
'mysqlconnection.Open()
Try
mysqlconnection.Open()
Catch myerror As MySqlException
MsgBox("Error connecting to database!")
Exit Sub
End Try
MsgBox("connected to database!")
End Sub