无法使用C#连接到其他机器的mysql服务器?

时间:2014-12-26 06:51:32

标签: c# mysql visual-studio-2010

我需要使用C#编码连接到其他机器的mysql服务器,下面是我的编码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MySql.Data.MySqlClient;

namespace c_mysql
{
    class Program
    {
        static void Main(string[] args)
        {
            string connection = "SERVER=192.168.1.5; Database=b2b; Uid=root;";
            MySqlConnection con = new MySqlConnection(connection);
            con.Open();
            MySqlCommand cmd = new MySqlCommand("select * from area",con);
            MySqlDataReader dr = cmd.ExecuteReader();
            if (dr.Read())
            {
                Console.WriteLine("connection successfull");
            }
            else
            {
                Console.WriteLine("not connected...");
            }
        }
    }
}

IP地址192.168.1.5是局域网中其他计算机的IP地址,我可以通过url栏中的192.168.1.5/phpmyadmin连接,但是当通过C#编码进行连接时,它说,

Host 'pc1' is not allowed to connect to this MySQL server

Pc 1是我正在编码的电脑。

请帮帮我。

1 个答案:

答案 0 :(得分:0)

启用对MySql的远程根访问。 click here for more details

从上面拍摄的片段:

为root创建一个新的HOST,并允许root从任何地方登录。

mysql> GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'password' WITH GRANT OPTION;
mysql> FLUSH PRIVILEGES;
mysql> exit