简体   繁体   中英

WPF MySQL connection error handling

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?

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

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?

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..

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM