【问题标题】:mysql using vb.net?mysql使用vb.net?
【发布时间】:2012-02-21 05:32:04
【问题描述】:

我在 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。

杰夫 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:无法连接到任何 指定的 MySQL 主机。在 MySql.Data.MySqlClient.NativeDriver.Open() 在 MySql.Data.MySqlClient.Driver.Open() 在 MySql.Data.MySqlClient.Driver.Create(MySqlConnectionStringBuilder 设置)在 MySql.Data.MySqlClient.MySqlPool.CreateNewPooledConnection() 在 MySql.Data.MySqlClient.MySqlPool.GetPooledConnection() 在 MySql.Data.MySqlClient.MySqlPool.TryToGetDriver() 在 MySql.Data.MySqlClient.MySqlPool.GetConnection() 在 MySql.Data.MySqlClient.MySqlConnection.Open() 在 mysql.Form1.Button3_Click(Object sender, EventArgs e) in C:\Users\pram\Documents\Visual Studio 2008\Projects\mysql\mysql\Form1.vb:第 48 行

在第 48 行“dataConnection.Open()”

【问题讨论】:

  • 我的代码可以与 .NET 中的 MySQL 数据库对话...但它在家里。如果你今晚没有得到合适的答案,我会找到并发布。
  • 仍然没有有效的答案请......帮帮我......有人吗?
  • 听起来您可能需要在“远程数据库访问主机”屏幕上查看您的访问主机。如果您的 IP 地址最近发生了变化(动态),这可能需要更新。您是否曾经从 SQL GUI(例如 HeidiSQL)连接到您的数据库?一旦我拨通了下面的代码就可以了。
  • @JeffV 是的,我尝试使用 SQL GUI(MySQL Workbench 5.2 CE)连接它说“在'读取初始通信数据包'时失去与 MYSQL 服务器的连接,系统错误:0”,我也是在远程数据库访问主机中添加了我的 ip,例如“xx.xx.%.%”,并且像这样添加到“%.%.%.%”仍然没有连接。帮忙??
  • 您曾经能够从 MySQL Workbench 远程连接吗?如果没有联系您的托管服务提供商的支持。先把它定位,然后再做代码。如果您远程连接,那么在代码中进行操作不会有问题。

标签: mysql vb.net


【解决方案1】:

我不确定那里发生了什么,但如果这是 MSSQL,我通常会检查协议、防火墙

【讨论】:

    【解决方案2】:

    试试这个。我正在使用这个 sn-p 来执行命令。

    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

    【讨论】:

    • 谢谢!但它在“objConn.Open()”行出现运行时错误,显示“无法连接到任何指定的 MySQL 主机”。为什么??
    【解决方案3】:

    这就是我的做法(使用 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 地址。

    【讨论】:

    • 谢谢!但是对不起!我是 C# 新手,你能把这段代码转换成 vb.net 吗?
    • 谢谢!我用 2 种类型的 sql 代码尝试你的代码,但没有 sql 代码但不工作!!!对不起!有什么想法吗??
    • 你引用的是:System.Data.Sql 和 MySql.Data.MySqlClient
    猜你喜欢
    • 2011-07-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多