PARTNER

Senin, 20 September 2010

HOW TO: Upgrade a version 3.x database file to SQL Server Compact 4.0

public static class SqlCeUpgrade{
    public static void EnsureVersion40(this System.Data.SqlServerCe.SqlCeEngine engine, string filename)
    {
        SQLCEVersion fileversion = DetermineVersion(filename);
        if (fileversion == SQLCEVersion.SQLCE20)
            throw new ApplicationException("Unable to upgrade from 2.0 to 4.0");

        if (SQLCEVersion.SQLCE40 > fileversion)
        {
            engine.Upgrade();
        }
    }
    private enum SQLCEVersion
    {
        SQLCE20 = 0,
        SQLCE30 = 1,
        SQLCE35 = 2,
        SQLCE40 = 3
    }
    private static SQLCEVersion DetermineVersion(string filename)
    {
        var versionDictionary = new Dictionary<int, SQLCEVersion> 
        { 
            { 0x73616261, SQLCEVersion.SQLCE20 }, 
            { 0x002dd714, SQLCEVersion.SQLCE30},
            { 0x00357b9d, SQLCEVersion.SQLCE35},
            { 0x003d0900, SQLCEVersion.SQLCE40}
        };
        int versionLONGWORD = 0;
        try
        {
            using (var fs = new FileStream(filename, FileMode.Open))
            {
                fs.Seek(16, SeekOrigin.Begin);
                using (BinaryReader reader = new BinaryReader(fs))
                {
                    versionLONGWORD = reader.ReadInt32();
                }
            }
        }
        catch
        {
            throw;
        }
        if (versionDictionary.ContainsKey(versionLONGWORD))
        {
            return versionDictionary[versionLONGWORD];
        }
        else
        {
            throw new ApplicationException("Unable to determine database file version");
        }
    }


}


Sample usage:

string filename = @"C:\Data\SQLCE\Northwind31.sdf";
var engine = new System.Data.SqlServerCe.SqlCeEngine("Data Source=" + filename);
engine.EnsureVersion40(filename);
 
source : http://erikej.blogspot.com/2010/08/how-to-upgrade-version-3x-database-file.html 
Share:

0 komentar:

Posting Komentar

Blog Archive

Blogger templates