title: Handling Internet Connection Hiccups and Database Connections
body: <p>I realize that there is no way to atomically guarantee:</p>
  
  <pre><code>if(hasInternet)
      doDatabaseCall();
  </code></pre>
  
  <p>However, <strong>what is the correct way of handling connection problems when dealing with DbConnection and DbCommand objects in the .NET world?</strong>  I'm specifically interested in the MySqlConnection and MySqlCommand objects but assume (hope) its pattern of exceptions is the same as SQL Server's classes.</p>
  
  <p>I'd assume that if the internet goes down before calling, conn.Open(), a MySqlException gets raised.  I'd also assume the same happens if you call ExecuteReader or ExecuteNonQuery and the internet has gone down.</p>
  
  <p>I'm not sure because the docs for the MySql objects don't say.  The SqlServer objects just say that it might raise a SqlException which means:</p>
  
  <blockquote>
    <p>An exception occurred while executing the command against a locked row. This exception is not generated when you are using Microsoft .NET Framework version 1.0.</p>
  </blockquote>
  
  <p>That doesn't seem to cover connection issues...  What I'd like to do is handle the exception, wait for some amount of time, and start over again.  My application's sole purpose is to execute these database calls and its running on a dedicated system so retrying forever is really the best option I believe.  That said, <strong>I would love to differentiate between connection exceptions and other kinds of database exceptions, is that possible?</strong></p>
  
  <p>I've done some testing and it appears to work as I assume but <strong>does it work in all edge cases?</strong>  Such as:  the command was successfully sent to the database server but the connection goes down before or while the results are being returned?  If it doesn't work in all edge cases then I'm going to have to: execute a command, query for the desired state change, execute the next command, etc.  It's important that each command goes through.</p>
  
  <p>I am connecting to a port on localhost that is forwarded via SSH to a remote server if that makes a difference.</p>