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.
abdus salamPosted Aug 7, 2007, 3:30 AM
I dont think that making a connection static is a secure and thread safe mechanism. This may create problems down the road if your application is web based (where multiple requests needs and open connection). Regarding pooling your understanding is pretty much right. The only thing to enable pooling for a particular connnection is to provide pooling specific property in the connection string (I believe you are already making things in this way).
Also setting connectionTimeOut property to the reasonable amount will make it sure that user will not see the "request time out" error and connection will be dequeue from the connection pool.
I hope this will help you out.
Munir ShaikhPosted Aug 7, 2007, 1:08 AM
I think you should check with DAAB, you will find SQLHelper.cs which will give you good idea as how you can handle SqlConnection
DAAB means (Microsoft Data Access Application Block)
Regards
>>Munnamax