views:

751

answers:

3

Keeping performance on a mobile device in mind, what do you think is the best approach with an SQLCE Connection. Keep it open for the duration of the application or let it close whenever a call to the database is needed.

Obviously this depends a little on the nature of your application however I'm curious to hear what the group has implemented and why.

+1  A: 

For my device, I kept it open during the lifetime of the application. If the functionality of the application depends on the SQLCE connection throughout, this makes sense.

Dave Markle
A: 

Get Late, Release Early. Open the Connection as late as possible in the operation, and close it as soon as is possible. Open SQL connections are not good.

StingyJack
That is true for a server database but not at all for an embedded database like SQL CE, which works in process with normally a single concurrent user.
Alejandro Mezcua
After working with access, I tend to use this model regardless of the product nature. =)
StingyJack
With Compact Edition, there's a relatively large overhead to opening a connection and you are "encouraged" by this fact to keep your connections around if you plan to reuse them.
Bob King
+5  A: 

You should definitely watch Steve Lasker's PDC08 session about SQL Server Compact Edition. There's a part about performance, best practices and his own benchmarks (~37:00).

In short, keep it open, try to reuse commands, and use SqlCeResultSet for bulk operations.

Martin Plante
Nice answer. Thanks slim!
Mat Nadrofsky
Also grabbed sample code for Lasker's Blog at: http://blogs.msdn.com/stevelasker/archive/2008/10/31/pdc-2008-embedding-sql-server-compact-in-desktop-and-device-applications.aspx
Mat Nadrofsky