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

為{幸葍}努か 提交于 2020-01-14 04:23:20

问题


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)


回答1:


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




回答2:


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.



来源:https://stackoverflow.com/questions/10750532/how-to-save-data-in-the-different-database-depending-on-the-internet-connection

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!