Unspecified error [ 5,sqlcese40.dll ] at System.Data.SqlServerCe.SqlCeConnection.ProcessResults(Int32 hr)

Posted by Blake on 11/17/2011

Uhm. The SQL Compact errors are pretty awful. I assume they make them awful so you want to use the full blown version of SQL Server. Of course, I would if that were an option available to me, which it's not. Anyway.

I obtained the above error when trying to open a SqlCeConnection. My problem was the user the web-site was running under didn't have permissions to access the database (even though it was in the App_Data directory). The user DID have access to read/write to Access databases in that same directory, so I can only assume that it was trying to invoke some action that needed additional permissions (even though MS swears you shouldn't need additional permissions).

Anyway, my solution was to use Impersonation before opening the connection and then end impersonation after closing the connection. This essentially executes that code segment with the privileges of the user your impersonating. I prefer to use it via code so only the minimum amount of code necessary runs with the additional security (as opposed to the entire page).

I have an open source project over at CodePlex to use Impersonation in code. It's a VB project but as with any .Net language you can use the compiled DLL in your C#, F# or *insert language here*. I believe the library on CodePlex targets 3.5 but the code should be able to be used in the framework versions 1.1-4.0.