I installed SQL Server Compact 3.5 x86 and started testing it out when I got a nice “Unable to load DLL ‘sqlceme35.dll” runtime exception. This seems like odd behaviour for a clean install. In a fairly quick search I found an article on the SQL Server Compact Team Blog that covers a couple possible causes/solutions for this exception.

http://blogs.msdn.com/sqlservercompact/archive/2007/10/26/can-t-find-p-invoke-dll-sqlcemenn-dll.aspx

My Solution - Target x86 Only

Even though there are 5 possible causes/solutions listed, only one of them was the issue I was experiencing and I think is probably the most common issue.

Quoted from the above post:

*If your machine is a 64-bit box, you might have been got trapped into default target platform ‘Any CPU’ trap, please set the target platform as ‘x86’. Need more details, please visit ErikEJ’s blog post. *

To fix it all you need to do is open up the Build Configuration Manager within Visual Studio and change the Active Solution Platform for your application from “Any CPU” to “x86”. Then rebuild and it’ll work just fine.

The reason for this issue according to ErikEJ’s post is “SQL Compact is only supported in 32 bit WoW (Windows on Windows) mode on the x64 platform.” From my understanding of how things work, when you compile for “Any CPU” your application will be run under the 64-bit .NET Framework on x64 Windows, and since SQL Compact is only 32-bit then Windows is unable to fire it up within the same process. This is because a single process in Windows can be either 32-bit or 64-bit and cannot run both 32-bit and 64-bit code within the same process. This is why it runs without throwing the exception once you change your app to target “x86” (32-bit).

What about 64-bit (x64) support?

Since the above SQL Server Compact Team Blog post was written, they have released a x64 version of SQL Server Compact 3.5. So x64 IS supported.

Now, if you distribute your application targeting “Any CPU” and bootstrap the SQL Compact x86 installer within your applications installer you will end up having issues if your users are running a 64-bit edition of Windows. So , to really support x64 Windows you really have two options:

  1. Change your application to target "x86" and distribute it that way. It'll run on both x86 and x64 editions of Windows.
  2. Alternatively, you could create two seperate versions of the installer for your application; one that targets x86 and one that targets x64.

I prefer the first option because it has a couple benefits. Even though it only targets x86, it will work on x64 systems too, and you relieve users from being required to understand whether they should install the x86 or x64 version. Plus this way you only need to worry about building your application and installer a single time for each release.

At the below link you can download both the x86 and x64 versions of SQL Server Compact 3.5 SP1:

http://www.microsoft.com/downloads/details.aspx?FamilyId=DC614AEE-7E1C-4881-9C32-3A6CE53384D9&displaylang=en