This is my "Upgrade if needed" code:
try
{
cn.Open();
}
catch (System.Data.SqlServerCe.SqlCeInvalidDatabaseFormatException ex)
{
//Try to upgrade
开发者_开发技巧 SqlCeEngine engine = new SqlCeEngine(strConnection);
engine.Upgrade(strConnection);
cn.Open();
}
It upgrades v3.5 db to v4.0 in practice. Is there any better solution? It would be nice if I detect installed engine & DB version in use.
See this for file version detection: How can I upgrade my Sql Server CE 3.5 sdf database to Sql Server CE 4.0?
And this code can detect the Engine versions installed:
internal static bool IsV40Installed()
{
try
{
System.Reflection.Assembly.Load("System.Data.SqlServerCe, Version=4.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91");
}
catch (System.IO.FileNotFoundException)
{
return false;
}
try
{
var factory = System.Data.Common.DbProviderFactories.GetFactory("System.Data.SqlServerCe.4.0");
}
catch (System.ArgumentException)
{
return false;
}
return true;
}
internal static bool IsV35Installed()
{
try
{
System.Reflection.Assembly.Load("System.Data.SqlServerCe, Version=3.5.1.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91");
}
catch (System.IO.FileNotFoundException)
{
return false;
}
try
{
var factory = System.Data.Common.DbProviderFactories.GetFactory("System.Data.SqlServerCe.3.5");
}
catch (System.ArgumentException)
{
return false;
}
return true;
}
精彩评论