Search code examples
c#exceptiondatabase-connectionmysql.data

Unable to connect to any of the specified MySQL hosts in WPF


I can't figure out what's wrong with the connection string. I tried all possible variations of the string. It's done in a WPF framework in vs 2015. I just installed SQL server 2016 newly.

The code is:

string ConnectionString = "Server=localhost;Port=3306;Database=EliseDB;Uid=root;Pwd=";
MySqlConnection conn = new MySqlConnection(ConnectionString);
conn.Open();

The error shown is:

'MySql.Data.MySqlClient.MySqlException' in MySql.Data.dll

An exception of type 'MySql.Data.MySqlClient.MySqlException' occurred in MySql.Data.dll but was not handled in user code

Additional Information:

Unable to connect to any of the specified MySQL hosts.

I even tried restarting TCP/IP in server config manager.


Solution

  • Reference: https://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlconnection(v=vs.110).aspx

    Something like the following:

    string connectionString = "Data Source=localhost, 3306;
    Initial Catalog=EliseDB;User ID=root"
    
    SqlConnection cnn = new SqlConnection(connectionString)
    
    try
    {
        cnn.Open();
        MessageBox.Show("Opened connection!");
        cnn.Close();
    }
    catch (Exception ex)
    {
        MessageBox.Show("Big Problem: " + ex.message);
    }