views:

112

answers:

1

Hello!

I'm developing a Windows Mobile WinForm application that uses Sql Server CE 3.1 with .NET Compact Framework 2.0 SP2 and C#.

I have a form that has a SqlCeConnection object, opened during all of it's execution time: I open the connection at startup and close it on event closing.

I also have a class to read the Sql Server CE database.

My question is about performance: Which of these two situations is the best?

1. When I create a object of reader class, pass to contructor the SqlCeConnection object and hold it into this new object as a property.

2. Always, when I call a method of this reader class, pass the SqlCeConnection object as a parameter.

I think if I use situations 1, I have two SqlCeConnection objects, isn't it?

If you need more details, tell me.

Thank you!

+1  A: 

To answer your last question first, no you won't have two different connections in scenario 1. The SqlCeConnection that you pass in is a reference, so the form and the reader class both have a reference to the same underlying connection object.

I actually don't think there's a huge amount of difference in the two scenarios. Since you're keeping the connection open for the lifetime of the form, it really doesn't matter if you pass it in the constructor of this secondary class or for each method. So I guess it's easier just to do scenario 1 that way the parameter list for each call is smaller.

This would matter if you were trying to minimize the lifetime of your connection. In, for instance, a Windows Desktop app connecting to a normal SQL Server, you wouldn't really want to hold open connections for the lifetime of the form because if lots of people were opening lots of forms, you've got performance problems on your server. So in that case, you would create the connection at the last second, pass it into your reader routine to get the data and immediately close it. Scenario 2 would be better for you in this case.

But that scenario doesn't seem important to you in a mobile application. You're probably the only one connecting to the database so it doesn't matter if you hold the connection open.

Clyde
I read somewhere that it's better to keep open the connection if I want a better performance.
VansFannel
I doubt you'll see any measurable performance difference either way. And in a full SQL Server environment, you would absolutely need to be worried about a performance DECREASE if all the clients are holding lots of connections open.
Clyde
Yes, but this is a single user scenario.
VansFannel