1

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.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Nime Cloud
  • 6,162
  • 14
  • 43
  • 75

1 Answers1

3

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;
    }
Community
  • 1
  • 1
ErikEJ
  • 40,951
  • 5
  • 75
  • 115