I want to make my code to be able to check if internet connection is established.
After that I will normally save records in the database on the server, but I want to be able to save records in the local database on pc everytime the connection is lost and before every normal connection on the server check if the local database is empty and copy everything from local database to server database.
Here is my code that I use now:
//open database connection
con = new MySqlConnection("server=192...;database=GPS_data;uid=root;pwd=******");
con.Open();
//check if card reader is loged
if (card_number != null)
{
cmd = new MySqlCommand("insert into data values (null, ?Parname , ?Parname2, ?Parname3, ?Parname4, ?Parname5, ?Parname6, ?Parname7);", con);
cmd.Parameters.Add("?Parname", MySqlDbType.Double).Value = Math.Round(deciLat, 5);
cmd.Parameters.Add("?Parname2", MySqlDbType.Double).Value = Math.Round(deciLon, 5);
cmd.Parameters.Add("?Parname3", MySqlDbType.Timestamp).Value = DateTime.Now;
cmd.Parameters.Add("?Parname4", MySqlDbType.VarChar).Value = card_number;
cmd.Parameters.Add("?Parname5", MySqlDbType.VarChar).Value = ConfigSettings.ReadSetting("reg");
cmd.Parameters.Add("?Parname6", MySqlDbType.VarChar).Value = ConfigSettings.ReadSetting("ser");
cmd.Parameters.Add("?Parname7", MySqlDbType.Double).Value = ellipHeight;
cmd.ExecuteNonQuery();
lastDBUpdate = DateTime.Now;
}
else //in the case when user is not logged in with the card
{
cmd = new MySqlCommand("insert into data values (null, ?Parname , ?Parname2, ?Parname3, ?Parname4, ?Parname5, ?Parname6, ?Parname7);", con);
cmd.Parameters.Add("?Parname", MySqlDbType.Double).Value = Math.Round(deciLat, 5);
cmd.Parameters.Add("?Parname2", MySqlDbType.Double).Value = Math.Round(deciLon, 5);
cmd.Parameters.Add("?Parname3", MySqlDbType.Timestamp).Value = DateTime.Now;
cmd.Parameters.Add("?Parname4", MySqlDbType.VarChar).Value = null;
cmd.Parameters.Add("?Parname5", MySqlDbType.VarChar).Value = ConfigSettings.ReadSetting("reg");
cmd.Parameters.Add("?Parname6", MySqlDbType.VarChar).Value = ConfigSettings.ReadSetting("reg");
cmd.Parameters.Add("?Parname7", MySqlDbType.Double).Value = ellipHeight;
cmd.ExecuteNonQuery();
lastDBUpdate = DateTime.Now;
}
So this part of the code goes on the server.
I mean there shouldn’t be any special connection check as this would probably result with an error if connection is not established.
I want to add saving to a local database depending on connection, so connection=lost ( save in the local databse), connection=established(first check if local database is empty= if not copy to server database, continue recording on server)
Create a function GetConnectionString() and use this function to get connection always.
Write down the code to check for internet existence and return the connection on basis of that.
Refer this for more code to check for internet connectivity 1
Refer this for more code to check for internet connectivity 2
Refer this for more code to check for internet connectivity 3