1

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?

4
  • 1
    Why not perform catches on the different Exception Types that MySQL connector can thrown. (Not exact Exception types) like try{}catch(MySqlConnectionException){}catch(MySqlQueryException ). You can catch and handle more than 1 type of exception, especially a general exception. Other than this you would need to drill into the General Exception and determine the underlying exception type. Take a look at this example as well dev.mysql.com/doc/connector-net/en/… Commented Jun 10, 2016 at 21:07
  • @Bearcat9425 too bad you answered this as comment and not an actual answer. I would have given you the credit. As it is all I can do is up vote the comment. If you do answer next time I check will give you the answer. Commented Jun 13, 2016 at 15:45
  • 1
    Posted my comment as an answer for reference to anyone else who comes upon this question. I know you have marked already an answer but figured I would put my version of the answer in. Commented Jun 13, 2016 at 17:51
  • 1
    I upvoted your question. Hope your excellent codes ! Commented Jun 14, 2016 at 6:27

3 Answers 3

3

Why not perform catches on the different Exception Types that MySQL connector can thrown. (Not exact Exception types) like

try
{
}catch(MySqlConnectionException)
{
}catch(MySqlQueryException )
{
}

You can catch and handle more than 1 type of exception, especially a general exception. Other than this you would need to drill into the General Exception and determine the underlying exception type. Take a look at this example as well

https://dev.mysql.com/doc/connector-net/en/connector-net-programming-connecting-errors.html

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

Comments

1

I am not aware of MySQL Exceptions, but you need to use more catch blocks.

just go to definition of each method you used and there you can see what exceptions could be thrown by each method. then instead of using

catch (Exception x)
    {
        //Amazing error catching code
    }

use multiple catch blocks, one after another. see this post for how to group exceptions if there are two many.

Catch multiple exceptions at once?

Comments

1

Just consider this as simple idea and if not needed, just ignore..There might be official way unlike this..

catch (MySqlException ex)
{
  if (ex.Message.Contains("connection"))
     {
       // do stuff when connection trouble
     }
  else
    {
       // do stuff when MySql trouble
    }
}

Hope this helps..

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.