2011-04-25 214 views
0

我試圖連接到一個數據庫,但它不工作...MySQL數據庫連接vb.net

代碼:

 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 

它alsways說,錯誤連接到數據庫!

有什麼問題?

+1

'再 conn.Open() 抓住myerror作爲個MySqlException MSGBOX(myerror.message) 結束Try' 這會給你更詳細的異常信息。 – Jack 2011-04-25 13:27:59

回答

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() 
+0

即使我這樣做'conn.ConnectionString =「Host = ***。*。**。***; user id = *******; password = ******」'它doesn不工作.. – Ruben 2011-04-25 10:47:47

2

首先確保安裝的MySQL連接器/ 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
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 
+3

歡迎來到StackOverflow。你的代碼有一些格式問題。通常還需要添加一些文字來解釋您的解決方案。您可以編輯您的帖子以進行這些更改。 – MicroVirus 2015-05-14 11:35:09