Consider a simple setup:
// _conn is the OdbcConnection with a MySQL-Server (MySQL-Connector 3.51)
// _cmd is a created OdbcCommand
// Constructor has created the objects successfully
public void DoSomething() {
if(_conn.State == ConnectionState.Open)
_cmd.ExecuteNonQuery();
}
Now my problem is, that OdbcConnection.State
isn't reliable. The thing is that after some time the connection gets lost, but the State
-Property doesn't know anything about it and keeps telling me that the connection is open at least till the point were I try to execute a command (which fails gracefully). I even had the situation were the State
-Property would never refresh and kept telling me that the connection was still there (but commands failed).
Of course I could add Try {...} Catch {...}
blocks to my code, but I tried to avoid them because extending a two-line function with at least four lines of error handling is a little bit...heavy.
So my question is: Why isn't OdbcConnection.State
reliable and can I fix it?