0

I'm getting error:

Could not establish a connection to database

when I connect with MySQL .NET connector. I'm fully sure of that the username is right, the password is right, the database is right, and the server is right. Here's the code:

mysqlCon.ConnectionString = "Server=(servername);Username=(username);Pwd=(rightpassword); 
Database=(rightdatabase)";
try
{
    mysqlCon.Open();
}
catch (Exception ex)
{
    MessageBox.Show("Could not establish a connection to database!");
}
1
  • It migh tbe usefull to print the ex.Message instead of ignoring it. If you don't use the exception you caught, don't give it a name (e.g. use catch(Exception) Commented Apr 9, 2011 at 14:33

3 Answers 3

1

Shouldn't you use Uid instead of Username? At least connectionstrings.com says so :)
And also check what port is your MySQL running at. You might need to add port number to your connection string if you aren't using the default one - 3306.

Sign up to request clarification or add additional context in comments.

Comments

1

I use:

public MySqlConnection NewConnection(string host, int port, string db, string user, string pwd)
{
    string cstr = String.Format(
        "SERVER={0};PORT={1};DATABASE={2};UID={3};PWD={4}",
        host, port, db, user, pwdd);
    MySqlConnection conn = new MySqlConnection(cstr);
    try { conn.Open(); }
    catch (Exception ex)
    {
        conn = null;
    }
    return conn;
}

Comments

0

Make sure that the database is reachable from the machine you're running the code.

Use telnet

telnet <server> 3306

or MySQL command line client

mysql -u <user> -h <server> -P <port> -p

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.