我正在为数据库系统构建移动伴侣应用程序。
由于移动应用程序无法始终访问数据连接,因此我需要在互联网不可用时将所有记录更改保存到平板电脑/手机上的本地数据库中。
我最初的想法是简单地使用像
这样的东西public bool TestConn()
{
SqlConnection conn = new SqlConnection("some connection string")
bool result;
try
{
conn.Open();
conn.Close();
result = true;
}
catch
{
MessageBox.Show("Data Connection not found");
result = false;
}
return result;
}
然后使用if
语句进行换行以在2个数据库之间切换,即。 if true use remote, else use local
有没有更好的方法来解决这个问题?我考虑使用try-catch系统,如果抛出的异常是它无法连接,那么它会保存到本地数据库(假设甚至可以完成)。考虑到可能抛出的潜在错误代码,这是否实用?