C# MySql.Data.MySqlClient.MySqlException (0x800040

2019-07-15 12:34发布

问题:

here's my connection string

"Server=xxx.xxx.xxx.x;Database=database;Uid='root';Pwd='';"

the ip on the server is the ip of the other computer(have xampp) that I want to access.

this is the Exception I receive.

MySql.Data.MySqlClient.MySqlException (0x80004005): 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.GetPooledConnection() at MySql.Data.MySqlClient.MySqlPool.TryToGetDriver() at MySql.Data.MySqlClient.MySqlPool.GetConnection() at MySql.Data.MySqlClient.MySqlConnection.Open()

回答1:

With mysql on C# you don't need single quote in the connexion string

Ex :

ConnStr = "Server = XXX.XXX.XXX.XXX; Database = mydatabase; Uid = root; Pwd = yourpassword";   

MySqlConnection con = new MySqlConnection(ConnStr)

You can also verify that you can ping the other computer :

    using System.Net.NetworkInformation;

    public static bool IsConnectedToServer()
    {
        Ping p = new Ping();
        try
        {
            PingReply reply = p.Send("XXX.XXX.XXX.XXX", 3000);
            if (reply.Status == IPStatus.Success)
                return true;
            else return false;
        }
        catch { return false; }
    }


回答2:

I think your connection string is wrong

I should be In <appSettings> in

<add key="myConnectionString" value="Server=localhost;Database=dbName;User Id=username;Password=password;"/>

or

in <connectionStrings>

<add name="myConnectionString" connectionString="Server=Server=localhost;Database=dbName;User Id=username;Password=password;" providerName="MySql.Data.MySqlClient"/>

As well as Refer this Link to Enable Remote Access for Mysql