Sunday, December 09, 2007

Detecting the SDF file version

SDF files don't have an explicit way of letting you know their versions. What can you do to tell the version of a given SDF file? In my products I detect the SDF file version as a side effect of opening it: if a given engine can open that SDF file then I can safely assume it has the same version as the engine. This is not a very straightforward approach because it requires that you have all engines installed on the device (not a typical scenario).

The alternative way to detect the SDF file version is by reading a few signature bytes on the file. Just open the file, set the read location at offset 16 and read the 32 bit int there. The following values will tell you the file version:
2.0: 0x73616261
3.0: 0x002dd714
3.5: 0x00357b9d

I wrote a very simple .NET CF 2.0 application to illustrate this. The download link is this. If you find any SDF file that fails this test, please let me know.

9 comments:

Scottie said...

Thanks for this! Why it has to be such a pain to find out the version number is beyond me...

João Paulo Figueira said...

It would be so much easier to have an API (or an officially documented way to do this)...

zalek said...

Starting at offset 16 (decimal 16) my .sdf file shows:
74 61 62 61

Zalek

João Paulo Figueira said...

Thank you for pointing this out. If memory serves me well, offset 16 points to the SQL CE 2.0 engine name. Are you using a localized version?

RudolfHenning said...

Just for completeness SQL compact 4.0 is 0x003D0900

DJ Burb said...

Thanks for this.... works like a charm

DJ Burb said...

Thanks for this.. works great

حسين ناجي الصفافير said...

Hi,
thanks for ur effort.
but can u add this:
0x003d0900 for SQLCE 4.0

Cameron OKeefe said...

Great work!