I am try do some error handling when it comes to connecting to a MySQL database. I want to separate out connection errors and SQL errors. Issue being is the error that is being returned doesn't seem to be a MySQL error but in fact a MS error. This is what I am doing:
using (MySqlConnection con = new MySqlConnection(ConString))
{
MySqlDataAdapter da = new MySqlDataAdapter();
MySqlCommand cmd = new MySqlCommand(sqlCMD, con);
try
{
System.Security.Cryptography.RSACryptoServiceProvider.UseMachineKeyStore = true;
var provider = new System.Security.Cryptography.RSACryptoServiceProvider();
con.Open();
da.SelectCommand = cmd;
da.Fill(dt);
con.Close();
}
catch (Exception x)
{
//Amazing error catching code
}
}
Now lets say I try to connect to a machine that does not have MySQL installed, this is the exception that being returned:
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()
Now googling "0x80004005 MySQL" returns multiple threads on Stack Overflow about C# connecting to MySQL. Just doing a search for 0x80004005 returns a Windows XP error message page.
So that leaves me with a question what is the best way to determine a connection error vs a SQL error message?