简体   繁体   中英

c# connect into MySQL

I'm trying to connect MySQL in C# develope in Expression Blend 4 but i get following error ?

try
        {
            string serverConnection = "SERVER=xxx.xxx.xxx.xxx; UID=xxx; PASSWORD=xxx; DATABASE=xxx;";
            MySqlConnection conn = new MySqlConnection(serverConnection);
            conn.Open();
            MessageBox.Show("Successfully connected");
            conn.Close();
        }
        catch (Exception ex)
        {
            MessageBox.Show("Error:"+ex.Message);
        }

Here is the error:

Unable to connect to any of the specified MySQL hosts

I think its more of like the placement of the parameters.

The standard format specified is this:

Server=myServerAddress; Port=1234; Database=myDataBase; Uid=myUsername; Pwd=myPassword;

You need to change it like this:

string serverConnection = "SERVER=xxx.xxx.xxx.xxx; DATABASE=xxx; UID=xxx; PASSWORD=xxx;";

Try to change and see if that works :)

The format for connection strings is as follows,

Server=myServerAddress; Port=1234; Database=myDataBase; Uid=myUsername; Pwd=myPassword;

The most likely issue is that you are trying to remotely connect to your MySQL server in which case make sure it allows for remote connections, and then you must check and make sure the user you are trying to login as is allowed to connect from either your public IP or any public IP.

NOTE: I know this response is 2 and 1/3 years late, but I wanted this to be here for anyone else with a similar issue.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM