Q:

How do i check whether connection details to MySql Server are valid?

I'm trying to write a function to check whether the connection details given on a Form are valid, so it returns True otherwise it returns False : 
How can i achieve this ?
My code below :
 private bool checkConnectionSuccess(string host, string user, string pass, string dbname) {
        string strProvider = "Data Source=" + host + ";Database=" + dbname + ";User ID=" + user + ";Password=" + pass;
        MySqlConnection myConn = new MySqlConnection(strProvider);
        myConn.Open();  

        if (________) return true; else return false;

    }

What should I do in order to Check for the connection wether it was succeded based on the given details (user, pass, dbname, host)
Thankyou 

A:

Use a try catch block:
private bool checkConnectionSuccess(string host, string user, string pass, string dbname)
{
   string connectionString = "Data Source=" + host + ";Database=" + dbname + ";User ID=" + user + ";Password=" + pass;

   bool sucessful = true;

   using (MySqlConnection conn = new MySqlConnection (connectionString))
   {
     try
     {
       conn.Open();
     }
     catch (MySqlException ex)
     {
       sucessful = false;
     }
   }
    return sucessful;
}

This is possible because MySQLConnection will likely fail on most exceptions, including if the credentials are wrong or the connection string is invalid.

