2012-02-21 83 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