PC Review


Reply
Thread Tools Rate Thread

What is best SQLCeConnection strategy?

 
 
Adam
Guest
Posts: n/a
 
      29th Oct 2007
hi
I would like to know whats better
To declare a SQLCeConnection variable as a Static member of
a Class containing functions to MakeConnection and
CloseConnection, or should I just declare it as a Public
member of the above mentioned class containing Connection
code.

And, if better is use a static SQLCeConnection should i use open a
connection once or open before a execute an query and close after it?


Thanks
 
Reply With Quote
 
 
 
 
Christian Resma Helle
Guest
Posts: n/a
 
      29th Oct 2007
I would use one static instance of it and just keep it open all the time.

--
Regards,
Christian Resma Helle
http://christian-helle.blogspot.com


"Adam" <(E-Mail Removed)> wrote in message
news:fg45pu$6fg$(E-Mail Removed)...
> hi
> I would like to know whats better
> To declare a SQLCeConnection variable as a Static member of
> a Class containing functions to MakeConnection and
> CloseConnection, or should I just declare it as a Public
> member of the above mentioned class containing Connection
> code.
>
> And, if better is use a static SQLCeConnection should i use open a
> connection once or open before a execute an query and close after it?
>
>
> Thanks



 
Reply With Quote
 
joker
Guest
Posts: n/a
 
      31st Oct 2007
I don't believe there is a simple answer to this question.

A single static connection may be the answer, but if your application
is multi-threaded and could have active connections to the database
from more than 1 thread you may have problems.

Someone already mentioned that closing the "last open connection" will/
may compact the database, but it also shuts down some db process(es).
Similar to shutting down the sql server services on a server or
desktop. Opening the first connection will start these process(es)

This is important to know because it has a huge impact on performance.

You'll find that opening and closing a single connection for each
query/transaction performs poorly due to the compacting and shutting
down of processes.

However, if you create a single sqlceconnection, open it, keep it
open, and never use it, for the lifetime of your application. You
will find that the opening and closing of subsequent sqlceconnections
performs very well and you may hardly notice the overhead of closing
and opening a connection. (watch out for the garbage collection when
disposing of many sqlceconnections)

Also keep in mind that an open connection locks the database file. So
if you need/want to be able to copy/remove that sdf file without
stopping your app, you'll need to close all the connections.

Knowing this will give you more flexibility in your approach. Good
luck.



On Oct 29, 5:47 am, "Christian Resma Helle" <xtian...@gmail.com>
wrote:
> I would use one static instance of it and just keep it open all the time.
>
> --
> Regards,
> Christian Resma Hellehttp://christian-helle.blogspot.com
>
> "Adam" <s...@wp.pl> wrote in message
>
> news:fg45pu$6fg$(E-Mail Removed)...
>
>
>
> > hi
> > I would like to know whats better
> > To declare a SQLCeConnection variable as a Static member of
> > a Class containing functions to MakeConnection and
> > CloseConnection, or should I just declare it as a Public
> > member of the above mentioned class containing Connection
> > code.

>
> > And, if better is use a static SQLCeConnection should i use open a
> > connection once or open before a execute an query and close after it?

>
> > Thanks- Hide quoted text -

>
> - Show quoted text -



 
Reply With Quote
 
Ginny Caughey [MVP]
Guest
Posts: n/a
 
      31st Oct 2007
> A single static connection may be the answer, but if your application
> is multi-threaded and could have active connections to the database
> from more than 1 thread you may have problems.


This is a good point. Each thread should have its own connection. Good
reply.

--
Ginny Caughey
Device Application Development MVP


"joker" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
>I don't believe there is a simple answer to this question.
>
> A single static connection may be the answer, but if your application
> is multi-threaded and could have active connections to the database
> from more than 1 thread you may have problems.
>
> Someone already mentioned that closing the "last open connection" will/
> may compact the database, but it also shuts down some db process(es).
> Similar to shutting down the sql server services on a server or
> desktop. Opening the first connection will start these process(es)
>
> This is important to know because it has a huge impact on performance.
>
> You'll find that opening and closing a single connection for each
> query/transaction performs poorly due to the compacting and shutting
> down of processes.
>
> However, if you create a single sqlceconnection, open it, keep it
> open, and never use it, for the lifetime of your application. You
> will find that the opening and closing of subsequent sqlceconnections
> performs very well and you may hardly notice the overhead of closing
> and opening a connection. (watch out for the garbage collection when
> disposing of many sqlceconnections)
>
> Also keep in mind that an open connection locks the database file. So
> if you need/want to be able to copy/remove that sdf file without
> stopping your app, you'll need to close all the connections.
>
> Knowing this will give you more flexibility in your approach. Good
> luck.
>
>
>
> On Oct 29, 5:47 am, "Christian Resma Helle" <xtian...@gmail.com>
> wrote:
>> I would use one static instance of it and just keep it open all the time.
>>
>> --
>> Regards,
>> Christian Resma Hellehttp://christian-helle.blogspot.com
>>
>> "Adam" <s...@wp.pl> wrote in message
>>
>> news:fg45pu$6fg$(E-Mail Removed)...
>>
>>
>>
>> > hi
>> > I would like to know whats better
>> > To declare a SQLCeConnection variable as a Static member of
>> > a Class containing functions to MakeConnection and
>> > CloseConnection, or should I just declare it as a Public
>> > member of the above mentioned class containing Connection
>> > code.

>>
>> > And, if better is use a static SQLCeConnection should i use open a
>> > connection once or open before a execute an query and close after it?

>>
>> > Thanks- Hide quoted text -

>>
>> - Show quoted text -

>
>


 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Strange(?) behaviour of SqlCeConnection Adam Microsoft Dot NET Compact Framework 1 11th Nov 2007 09:57 PM
SqlCeConnection closed after calling Open() Jan Obrestad Microsoft Dot NET Compact Framework 1 11th Apr 2006 06:46 PM
Performance with SqlCeConnection =?Utf-8?B?SnVsaW8gVHN1a2Ftb3Rv?= Microsoft Dot NET Compact Framework 2 11th Mar 2005 10:39 AM
SQLCEConnection Rodrigo Microsoft C# .NET 0 11th May 2004 05:34 PM
Re: Native Error on sqlceconnection open Ravishankar VB Microsoft Dot NET Compact Framework 0 24th Jul 2003 04:35 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 04:21 AM.