Question

How do I get changes to be committed to the .sdf?

If the app crashes then all changes to the database are left in a temp file and do not get committed to the database .sdf.

Do I need to close and reopen the connection, or is there a setting or better way?

My app is designed to work with either SQL Server CE or "normal" SQL Server, so it uses ProviderFactory to get connections. When using SQL Server Express, the connection is opened and closed with every transaction, and all changes are committed immediately.

But with SQL Server CE, on a few machines, there is a 30-second delay while SQL Server CE is opened (the cause seems to be access-permissions on a .NET encryption file. SEE: SQL Server CE extremely slow on some computers)

So, with SQL Server CE, I leave the connection open for the whole duration app, but if it crashes or I stop it with the debugger, then the DB changes don't get saved.

Database file is in ApplicationData area.

I'm using SQL Server CE 4.0, private deployment.

Thanks for your help!

Was it helpful?

Solution

First of all, you should prevent your app from crashing! You can implement global unhandled excpetion handler, if nothing else, and there force data to flush (or at least prevent the app from closing too early).

By default, the SQL Compact engine can wait up to 10 seconds before flushing to disk. This is controlled by the "Flush Interval" connection string setting, whcih can be as low as 1 second. So doing that may be a good start.

You can also make SQL Compact flush data immediately, by using the SqlCETransaction Commit mode "Immediate" - see my blog pos here for a full sample: http://erikej.blogspot.dk/2013/05/sql-server-compact-code-snippet-of-week_21.html

Both of these option will affect perceived performance.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top