I am planning to code an application in C# that will only allow a single logged in user connection at a time. I will have a bit column in my SQL Database called LoggedOn and when logging in I will make it check if the database says loggedon = true such as.
if (Loggedon == true) {
//Login things
} else {
//it appears you are already logged on.
}
My problem is what if my program unexpectedly shuts down?
For example: Force shutdown or task manager end process, how can I run a query before someone does that to prevent no changes to my database.
I am not sure if I explained this well enough but heres my code
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
string myConnection = "******;";
MySqlConnection conn = new MySqlConnection(myConnection);
conn.Open();
MySqlCommand comm = conn.CreateCommand();
comm.CommandText = "INSERT INTO users LoggedOn = 'False' where username = '" + Form1.TextBoxText + "'";
comm.ExecuteNonQuery();
conn.Close();
base.Dispose(disposing);
}
Will this code cover all unexpected closes?is there any alternative easy fast ways of doing this?
Would an eventhandler fix my issue? such as this: http://msdn.microsoft.com/en-us/library/system.appdomain.unhandledexception.aspx