I wrote simple console application (based on "HOW TO: Upgrade a version 3.x database file to SQL Server Compact 4.0") to upgrade (convert) CE databases with previous verions to SQL Server Compact 4.0 There is the code: or you can download whole project of the tool.
/// Program.cs using System; using System.Data.SqlServerCe; namespace CEUpdate { class Program { static void Main(string[] args) { Console.Write(@"Source DB (*.sdf file Ex.: C:\Database.sdf): "); string sourceDB = Console.ReadLine().Trim(); Console.Write("Password (If DB is password protected, otherwise hit Enter): "); string password = Console.ReadLine().Trim(); Console.Write(@"Destination DB (*.sdf file Ex.: C:\temp.sdf): "); string destinationDB = Console.ReadLine().Trim(); if (string.IsNullOrEmpty(sourceDB)) { Console.WriteLine("Error: source DB can't be empty."); Console.Write("Press any key to exit..."); Console.ReadKey(); } else if (string.IsNullOrEmpty(destinationDB)) { Console.WriteLine("Error: destination DB can't be empty."); Console.Write("Press any key to exit..."); Console.ReadKey(); } else if (sourceDB.Equals(destinationDB, StringComparison.CurrentCultureIgnoreCase)) { Console.WriteLine("Error: destination and source can't be the same."); Console.Write("Press any key to exit..."); Console.ReadKey(); } else { string connection = "Data Source=\"" + sourceDB + "\""; if (!string.IsNullOrEmpty(password)) connection += ";Password=" + password; using (var engine = new SqlCeEngine(connection)) { engine.EnsureVersion40(sourceDB, destinationDB); } Console.Write("Done! \nPress any key to exit..."); Console.ReadKey(); } } } }
/// SqlCeUpgrade.cs using System; using System.Collections.Generic; using System.IO; using System.Data.SqlServerCe; public static class SqlCeUpgrade { public static void EnsureVersion40(this SqlCeEngine engine, string source, string destination) { SQLCEVersion fileversion = DetermineVersion(source); if (fileversion == SQLCEVersion.SQLCE20) throw new ApplicationException("Unable to upgrade from 2.0 to 4.0"); if (SQLCEVersion.SQLCE40 > fileversion) { engine.Upgrade(String.Format("Data Source=\"{0}\"", destination)); } } 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"); } } }
Sources: