The opinions expressed herein are my own personal opinions. They are not necessarily fact or sactioned by any other person or organization. If you disagree that's your right. It's also my right to not care.
© Copyright 2008, Chris Tacke
In a recent project we decided to migrate the application's data engine from SQL CE 3.1 to SQL CE 3.5. The challenge with this is that the file formats themselves are not compatible and trying to connect to a 3.1 database usign the 3.5 objects will cause an Exception. 3.5 provides a method to upgrade 3.1 database files, but to use it you really need to know that the file is in 3.1 format to begin with.Of course you could always put in logic to connect and catch the exception and then upgrade, but let's face it - using exceptions for flow control is a bad practice and any ime it can be avoided it should be. Surprisingly, the SQL CE team didn't give us any mechanism otherwise to determine a database file's version, so I put together the following (which should work on the desktop as well as the device):
internal enum SSCEVersion{ Unknown, v3_1, v3_5}internal static SSCEVersion GetDatabaseVersion(string path){ uint signature = 0; using (FileStream stream = new FileStream(path, FileMode.Open)) { using (BinaryReader reader = new BinaryReader(stream)) { stream.Seek(16, SeekOrigin.Begin); signature = reader.ReadUInt32(); } } switch (signature) { case 0x00357b9d: // 3.5 return SSCEVersion.v3_5; case 0x002dd714: // 3.1 return SSCEVersion.v3_1; default: return SSCEVersion.Unknown; }}
Remember Me