0

我尝试连接到我朋友创建的服务器上的 MySQL 数据库,但它仍然说它无法连接到服务器!

这是我的代码:

        String connectionString = "Persist Security Info=False;database=qwertyui;server=www.qwertyuiop.com;Uid=asdfghj;Pwd=qazwsxedc;Connect Timeout=30";
        using (SqlConnection connection = new SqlConnection(connectionString))
        {
            SqlCommand cmd = new SqlCommand("INSERT INTO etc.... ");
            cmd.CommandType = CommandType.Text;
            cmd.Connection = connection;
            cmd.Parameters.AddWithValue( etc.... );
            connection.Open();
            int r = cmd.ExecuteNonQuery();
        }

它在connection.Open()抛出异常的指令上失败 -> 无法连接到服务器。

请问有什么想法吗?

4

4 回答 4

2

SqlConnection是一个SQL Server客户端。

您需要从 NuGet 下载 MySql 客户端。

于 2013-07-22T21:50:26.127 回答
1
  • 首先下载MySQL 连接器

  • 确保将 a 添加reference到 DLL(+ set copy local to true!)

  • 添加using MySql.Data.MySqlClient;在您的班级之上。

  • 至于前面Connection String,不要加or 。_ 因为它将尝试连接到 apache(端口 80)而不是 MySQL。的默认端口是.httpwwwserverMySQL3306

    string connectionString = "Persist Security Info=False;database=qwertyui;server=qwertyuiop.com;Uid=asdfghj;Pwd=qazwsxedc;port=3306";
    using (MySqlConnection connection = new MySqlConnection(connectionString))
    {
        string query = "INSERT INTO table VALUES (@parameter)";
        connection.Open();
        using (MySqlCommand cmd = new MySqlCommand(query, connection))
        {
            cmd.Parameters.AddWithValue("@parameter", parameter);
            cmd.ExecuteNonQuery();
        }
    }
    

如果这不起作用。确保MySQL 允许远程访问,如果不是这种情况,您可以一直尝试而没有任何结果。

于 2013-07-23T02:33:14.630 回答
0

首先想到的是您必须使用 MysqlConnection 和命令。 http://dev.mysql.com/doc/refman/5.0/es/connector-net-examples-mysqlconnection.html

是本地数据库吗?

于 2013-07-22T21:54:32.820 回答
0

如果以上所有方法都不起作用,请尝试授予所有权限(GRANT 语法)!

基本上它会像这样:

GRANT ALL PRIVILEGES ON DBname.* to 'username'@'IPadress' IDENTIFIED BY 'password';
于 2013-07-23T12:36:52.323 回答