I think the first thing to solve is why should the connection be open for 8 hours in a row?
I think that in most cases you will connect to the database, do your work and close it:
using (var conn = new MySqlConnection(connString)) {
The using block will create a connection that acts only in the block. When the end is complete, the Dispose method will be called on the connection and close and properly dispose of the connection object. This will prevent your application from using available connections when they are not needed.
If you are in a situation where you need to determine whether the connection will be open, your connection variable will have the State property, which will be the ConnectionState enumeration.
You can verify this using something like this:
if (conn.State != ConnectionState.Open) { conn = new MySqlConnection(connString); }
Hope this helps.
source share