SqlCeConnection guidelines - keep it open?

What is exactly the recommended way of using a SqlCeConnection object in your application

In all the examples I see (IBuySpyDelivery for example) it opens the connection object in the constructor and leaves it open most of the time until the class gets disposed.

Is that the way to do it Or should you open and close the connection for each database action (insert/select/delete) you're doing on the local SQL Mobile database

I'm a bit confused, but maybe I'm messing up the way of working on remote database servers with how it should be done on a local SQL Mobile db.



Answer this question

SqlCeConnection guidelines - keep it open?

  • SHESHAGIRI

    Are there any examples for creating a thread-safe wrapper class

    I see things like Synchronized, SyncRoot and locks, but I'm not sure if and how I should implement them.


  • Ariston Darmayuda

    Okay, after reading both links I'm still a bit confused.

    What I understand from the blog post it is better to use one connection object which gets opened at the start and stays open all the time until you dispose the local database class/wrapper.

    But the thread from the second link then says that SqlConnection objects (and in this case also SqlCeConnection) are not thread safe. So in case of a multi-threaded app you should use a seperate connection object for each database action (create one at the start of the function and dispose it at the end).

    Am I right with this conclusion


  • nugget2

    Take a look at this blogpost. The compact framework doesn't support connection pooling, so i think you need to test both cases. I depence on how ofter you want communicate with the database.

    Meybe this thread is a good read to, it's not about the SQLCeConnection but will give you some info about this topic.


  • camelo

    You are right, but you can make them Thread-Safe yourself offcoure. Just make your wrapper Thread-Safe.
    That should do the trick


  • José.Alvarez

    Do you use it within more threads and/or Is your application multi-threaded

    Because if it isn't, you don't need a Thread-Safe wrapper class. You can't write a multi-threaded application without having the knowlage about synchronisation.

    For more about synchronization, read:



  • SqlCeConnection guidelines - keep it open?