【发布时间】:2016-11-03 12:17:58
【问题描述】:
我的应用实际上使用SqlServerCe(Microsoft SqlServer Compact)。现在有了我的新更新,我换成了SQLite。
现在我的问题是:每次用户要导入数据库时,他可以导入SqlServerCe 文件(旧备份)或SQLite(新备份)。如何检测我的DbConnection 需要哪个DbProviderFactory?
旧方法(需要更新)
/// <summary>
/// Verifies the db if it is not corrupt! If the return value is <see cref="Nullable"/>, the DB is corrupt!
/// </summary>
/// <returns><see cref="Array"/> of <see cref="int"/>. The 1. index is Components.Count(), the 2 index is the Recipes.Count()!!</returns>
[CanBeNull]
public static int[] ImportDB_Verify()
{
try
{
SqlCeProviderFactory provider = new SqlCeProviderFactory();
SqlCeConnectionStringBuilder connectionStringBuilder = new SqlCeConnectionStringBuilder
{
DataSource = "Path/to/foo.db"
};
int[] val = new int[2];
using (DbConnection dbConnection = provider.CreateConnection())
{
dbConnection.ConnectionString = connectionStringBuilder.ConnectionString;
dbConnection.Open();
using (DbCommand dbCommand = dbConnection.CreateCommand("SELECT Count(*) FROM Components"))
{
val[0] = (int)dbCommand.ExecuteScalar();
}
using (DbCommand dbCommand = dbConnection.CreateCommand("SELECT Count(*) FROM Recipes"))
{
val[1] = (int)dbCommand.ExecuteScalar();
}
}
return val;
}
catch (Exception ex)
{
_Logger.Error(ex);
return null;
}
}
尝试捕捉“解决方案”
如果有更好的,请告诉我!
/// <summary>
/// Verifies the db if it is not corrupt! If the return value is <see cref="Nullable"/>, the DB is corrupt!
/// </summary>
/// <returns><see cref="Array"/> of <see cref="int"/>. The 1. index is components.Count(), the 2 index is the recipes.Count()!!</returns>
[CanBeNull]
public static int[] ImportDB_Verify()
{
int[] val = new int[2];
Exception sqLiteException;
try
{
SQLiteFactory provider = new SQLiteFactory();
SQLiteConnectionStringBuilder connectionStringBuilder = new SQLiteConnectionStringBuilder
{
DataSource = Core.CommonAppDataPath + "tmp.HTdb"
};
using (DbConnection dbConnection = provider.CreateConnection())
{
dbConnection.ConnectionString = connectionStringBuilder.ConnectionString;
dbConnection.Open();
using (DbCommand dbCommand = dbConnection.CreateCommand("SELECT Count(*) FROM components;"))
{
val[0] = (int)dbCommand.ExecuteScalar();
}
using (DbCommand dbCommand = dbConnection.CreateCommand("SELECT Count(*) FROM recipes;"))
{
val[1] = (int)dbCommand.ExecuteScalar();
}
}
return val;
}
catch (Exception ex)
{
sqLiteException = ex;
}
try
{
SqlCeProviderFactory provider = new SqlCeProviderFactory();
SqlCeConnectionStringBuilder connectionStringBuilder = new SqlCeConnectionStringBuilder
{
DataSource = Core.CommonAppDataPath + "tmp.HTdb"
};
using (DbConnection dbConnection = provider.CreateConnection())
{
dbConnection.ConnectionString = connectionStringBuilder.ConnectionString;
dbConnection.Open();
using (DbCommand dbCommand = dbConnection.CreateCommand("SELECT Count(*) FROM Components;"))
{
val[0] = (int)dbCommand.ExecuteScalar();
}
using (DbCommand dbCommand = dbConnection.CreateCommand("SELECT Count(*) FROM Recipes;"))
{
val[1] = (int)dbCommand.ExecuteScalar();
}
}
return val;
}
catch (Exception ex)
{
_Logger.Error(ex, $"Error while verifying the database. The SQLite Exception: {sqLiteException}");
return null;
}
}
【问题讨论】:
-
提醒一下:您可以使用
SqlCeEngine.Verify方法验证SQL Ce数据库的完整性。