我有这个密码:
//open database connection on the server
con = new MySqlConnection("server=localhost;database=GPS;uid=root;pwd=****");
con.Open();
if (con.State == ConnectionState.Closed)
{
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;
if (card_number != null)
{
cmd.Parameters.Add("?Parname4", MySqlDbType.VarChar).Value = card_number;
}
else
{
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("ser");
cmd.Parameters.Add("?Parname7", MySqlDbType.Double).Value = ellipHeight;
cmd.ExecuteNonQuery();
lastDBUpdate = DateTime.Now;
}
else
{
// local mysql database
con = new MySqlConnection("server=localhost;database=temp_GPS;uid=root;pwd=******");
con.Open();
if (con.State == ConnectionState.Open)
{
cmd = new MySqlCommand("insert into temp_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;
if (card_number != null)
{
cmd.Parameters.Add("?Parname4", MySqlDbType.VarChar).Value = card_number;
}
else
{
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("ser");
cmd.Parameters.Add("?Parname7", MySqlDbType.Double).Value = ellipHeight;
cmd.ExecuteNonQuery();
lastDBUpdate = DateTime.Now;
}
}
如果例如连接状态正常, 如果连接状态正常, 服务器已建立连接, 我想使我的代码工作, 然后在服务器上保存结果, 如果连接到服务器失败, 然后连接到本地数据库, 并且保存到本地数据库。 本地数据库总是可以访问的 。
有人能安排我的代码 工作,因为我想,我不知道 如何实现它。
谢谢