you must close the connection even is broken.
Possible values for ConnectionState
JavaScript:
if (!General.db.IsConnected)
{
General.log("[write2db] dbase is not connected");
if (General.db != null)
{
General.log("[write2db] dbase is not null");
try
{
//https://stackoverflow.com/a/31174539/1320686
General.db.GetConnection().Close();
}
catch (Exception x) {
General.log("[write2db] dbase is not null, tried to close but error occurred : " + x.Message);
}
}
General.log("[write2db] trying to open dbase");
/* READ CONFIG + CONNECT */
string db_res = connect2db();
if (db_res.Length > 0)
{
General.log("[write2db] trying to open dbase but error occurred : " + db_res);
MessageBox.Show(db_res, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);
Environment.Exit(-1);
return;
}
/* READ CONFIG + CONNECT */
General.log("[write2db] connection success!");
}
//proper way to check if is connected
public bool IsConnected
{
//src - https://stackoverflow.com/a/34357667/1320686
if (objConn == null | !objConn.State.HasFlag(ConnectionState.Open)) //INVALID ==> .State != ConnectionState.Open)
{
return false;
}
else
{
return true;
}
}
Possible values for ConnectionState
JavaScript:
//https://msdn.microsoft.com/en-us/library/system.data.connectionstate(v=vs.110).aspx
Broken - The connection to the data source is broken. This can occur only after the connection has been opened. A connection in this state may be closed and then re-opened.
Closed - The connection is closed.
Connecting - The connection object is connecting to the data source.
Executing - The connection object is executing a command.
Fetching - The connection object is retrieving data.
Open - The connection is open.