user123_456
user123_456

Reputation: 5805

How to save data in the different database depending on the internet connection

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)

Upvotes: 1

Views: 721

Answers (2)

Romil Kumar Jain
Romil Kumar Jain

Reputation: 20775

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.

public string GetConnectionString()
    {
        string SqlConString1 = value;//Read from config
        string SqlConString2 = value;//Read from config
        WebClient client = new WebClient();
        try
        {
            using (client.OpenRead("http://www.google.com"))
            {
            }
            return SqlConString1 ;
        }
        catch (WebException)
        {
            return SqlConString2 ;
        }
    }

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

Upvotes: 3

Denis Kucherov
Denis Kucherov

Reputation: 379

Create function that returns connection object depends of any logic. Then make transaction to this object and always check result. If result is not ok, then depends on connection type make dession what to do with data.

Upvotes: 0

Related Questions