2012-02-21 42 views
1

我在VB.NET中開發了一個項目mysql使用vb.net?

在這個項目中我想使用駐留在我的WEB服務器上的MySQL數據。

我可以與我的本地主機的MySQL服務器通信,但不能與WEB服務器通信。

在我的cPanel我添加主機從遠程數據庫訪問

但我不能與WEB的MySQL服務器進行通信。

請幫幫我。

Dim connection As MySqlConnection 
    connection = New MySqlConnection() 

    connection.ConnectionString = "Server=saver ip; Port=2082; Uid=username; Pwd=password; Database=database; Connect Timeout=60;" 

    Try 
     connection.Open() 
     MessageBox.Show("Connection Opened Successfully") 
     connection.Close() 
    Catch mysql_error As MySqlException 
     MessageBox.Show("Error Connecting to Database: " & mysql_error.Message) 
    Finally 
     connection.Dispose() 
    End Try 

當我嘗試運行這個。我得到了這個錯誤「錯誤連接到數據庫:從流中讀取失敗。」

注意*:我的數據庫名稱如「myweb_dbname」和我的用戶名「myweb_username」是這樣可以嗎?我正在使用cPanal1.0(RC1)和mysql5.1.56-log和os linux。

Jeff V:謝謝!當我嘗試你的代碼..

Dim dataConnection As New MySql.Data.MySqlClient.MySqlConnection() 
dataConnection.ConnectionString = "Server = xx.xx.xxx.xxx; Database = dbNAME; Uid = userID; Pwd = password;" 


Dim dataCommand As MySql.Data.MySqlClient.MySqlCommand = New MySqlCommand() 
dataCommand.Connection = dataConnection 


Try 
    dataConnection.Open() 
    dataConnection.Close() 
Catch x As Exception 
    Console.WriteLine(x.Message.ToString()) 
    MsgBox(x.ToString) 
End Try 

我收到此錯誤信息:

MySql.Data.MySqlClient.MySqlException: Unable to connect to any of the specified MySQL hosts. at MySql.Data.MySqlClient.NativeDriver.Open() at MySql.Data.MySqlClient.Driver.Open() at MySql.Data.MySqlClient.Driver.Create(MySqlConnectionStringBuilder settings) at MySql.Data.MySqlClient.MySqlPool.CreateNewPooledConnection() at MySql.Data.MySqlClient.MySqlPool.GetPooledConnection() at MySql.Data.MySqlClient.MySqlPool.TryToGetDriver() at MySql.Data.MySqlClient.MySqlPool.GetConnection() at MySql.Data.MySqlClient.MySqlConnection.Open() at mysql.Form1.Button3_Click(Object sender, EventArgs e) in C:\Users\pram\Documents\Visual Studio 2008\Projects\mysql\mysql\Form1.vb:line 48

在48行 「dataConnection.Open()」

+0

我有一個從.NET ......會談到一個MySQL數據庫代碼,但它是在家裏。如果今晚你沒有得到合適的答案,我會找到併發布。 – webdad3 2012-02-21 17:24:18

+0

謝謝!非常! @JeffV – pram 2012-02-21 17:37:34

+0

仍然沒有工作的答案plz ....幫我...有人嗎? – pram 2012-02-22 13:11:16

回答

0

,我不知道是怎麼回事但如果這是MSSQL,我通常會檢查協議,防火牆

+0

我已經禁用了防火牆! – pram 2012-02-21 06:21:02

0

這是我如何做到的(使用C#):

using System.Data.Sql; 
using MySql.Data.MySqlClient; 

     MySql.Data.MySqlClient.MySqlConnection dataConnection = new MySql.Data.MySqlClient.MySqlConnection(); 
     dataConnection.ConnectionString = "Server = xx.xx.xxx.xxx; Database = dbNAME; Uid = userID; Pwd = password;"; 


     MySql.Data.MySqlClient.MySqlCommand dataCommand = new MySqlCommand(); 
     dataCommand.Connection = dataConnection; 

     //tell the compiler and database that we're using parameters (thus the @first, @last, @nick) 
     dataCommand.CommandText = ("INSERT INTO ...;"); 

     //add our parameters to our command object 
     dataCommand.Parameters.AddWithValue("@recordId", dataString[0].ToString()); 
     ... 

     try{ 
      dataConnection.Open(); 
      dataCommand.ExecuteNonQuery(); 
      dataConnection.Close(); 
     }catch (Exception x){ 
      Console.WriteLine(x.Message.ToString()); 
     } 

我以爲我可能最初在VB.Net,但你「應該」能夠很容易地將其轉換爲VB。如果你仍然需要幫助,請告訴我。我會盡量把它轉換成今晚。

讓我知道它是否適合你。

更新 - VB.NET代碼:

Dim dataConnection As New MySql.Data.MySqlClient.MySqlConnection() 
dataConnection.ConnectionString = "Server = xx.xx.xxx.xxx; Database = dbNAME; Uid = userID; Pwd = password;" 


Dim dataCommand As MySql.Data.MySqlClient.MySqlCommand = New MySqlCommand() 
dataCommand.Connection = dataConnection 

'tell the compiler and database that we're using parameters (thus the @first, @last, @nick) 
dataCommand.CommandText = ("INSERT INTO ...;") 

'add our parameters to our command object 
dataCommand.Parameters.AddWithValue("@recordId", dataString(0).ToString()) 

Try 
    dataConnection.Open() 
    dataCommand.ExecuteNonQuery() 
    dataConnection.Close() 
Catch x As Exception 
    Console.WriteLine(x.Message.ToString()) 
End Try 

我用這個轉換工具(http://www.developerfusion.com/tools/convert/csharp-to-vb/) - 因此,嘗試這一點。

更新 - 回答問題的留言:

您可以使用命令提示符ping您的網站:

cmd.exe 

然後在你的網站URL的命令窗口類型。這會讓你回到你的網站的IP地址。

+0

謝謝!但對不起!我是C#的新手,您可以將此代碼轉換爲vb.net嗎? – pram 2012-02-22 15:18:44

+0

謝謝!我嘗試你的代碼與2類型的SQL代碼與SQL代碼,但不工作!抱歉!任何想法?? – pram 2012-02-23 04:46:54

+0

你引用:System.Data.Sql和MySql.Data.MySqlClient – webdad3 2012-02-23 05:31:48

0

試試這個。我正在使用此片段執行命令。

Imports MySql.Data.MySqlClient 
Dim strConnString As String = "server=xxx.x.x.x;uid=user;pwd=password;database=xxx" 
Dim objConn As New MySqlConnection(strConnString) 

Public Function MyDataSet(ByVal strSQL As String) As DataSet 
     Dim ds As New DataSet 
     Dim mycmd As MySqlCommand = New MySqlCommand(strSQL, objConn) 
     Dim ad As MySqlDataAdapter = New MySqlDataAdapter 
     ad.SelectCommand = mycmd 
     objConn.Open() 
     ad.Fill(ds, "a") 
     objConn.Close() 
     Return ds 
End Function 

最好的問候, @iamsupergrasya

+0

謝謝!但是它在「objConn.Open()」運行時出錯,表示「無法連接到任何指定的MySQL主機」。爲什麼?? – pram 2012-02-23 08:06:24