Program development question

I am 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 open throughout its entire execution: I open the connection on startup and close it when the event is closed.

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

My performance question is: Which of these two situations is better?

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

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

I think that if I use situation 1, I have two SqlCeConnection objects, right?

If you need more information, please tell me.

Thank!

+2


source to share


1 answer


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

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



This would be important if you were trying to minimize the lifespan of your connection. For example, if a Windows Desktop application connects to a regular SQL Server, you would not want to have open connections for the lifetime of a form, because if many people open many forms, you are having performance issues with your server. So, in this case, you have to create the connection at the last second, pipe it to your reader to get the data, and close it immediately. Scenario 2 would be better for you in this case.

But this scenario doesn't seem important to you in a mobile app. You are probably the only one connecting to the database, so it doesn't matter if you keep the connection open.

+1


source







All Articles