sqlConnection guidance.

Jul 13 2007 6:25 AM

Hi All,

I'm after a little guidance regarding SQL database access in C#.
I come from a VB6 background and rightly or wrongly the way i was shown database access with ADO was to open a global connection at the beginning, use it throughout the program and close it down at the end or if a problem occurred.

I understand now that keeping a connection open for the whole time is wrong but i would like to understand what the correct way in C# is.

At the moment i have created a single static connection in a static class and i've set the ConnectionString property at the very beginning though i don't open the connection. Then throughout the program i open the connection, perform the work and then close it afterwards.

Now, should i be doing it this way or should i be creating a new connection at every point where database access is required ? I take it that as long as the ConnectionString remains unchanged, a connection will be taken from the pool?? If this is correct, would i simply have a static variable that has my ConnectionString and then pass it as a parameter every time a new connection is created?

Thanks in advance.
Matt.


Answers (2)