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
internal static SSCEVersion GetDatabaseVersion(string path)
uint signature = 0;
using (FileStream stream = new FileStream(path, FileMode.Open))
using (BinaryReader reader = new BinaryReader(stream))
signature = reader.ReadUInt32();
case 0x00357b9d: // 3.5
case 0x002dd714: // 3.1