DB connection - should i close it?

moein_ak

Member
Joined
Aug 26, 2010
Messages
6
Programming Experience
5-10
Hello everyone
I had a little question.
When i open a connection in vb.net should i close it after using it and open it again when i need it or let it be open the whole time until the program is terminated?
Please answer with reason
thanks
 

cjard

Well-known member
Joined
Apr 25, 2006
Messages
7,052
Programming Experience
10+
Close it, because Microsoft say that's what you should do and they know best, because they wrote the language. ADO.NET uses a mechanism that makes database access as efficient as possible. It relies on you doing what Microsoft tell you to - opening and closing connections only when needed.

If you turn off that mechanism (Connection Pooling) then open/close will cause a performance hit, but first you should have a justified reason for turning off pooling..

Personally, i just use their modern, advocated way of doing data access (read the DW4 link in my signature) and the code that visual studio generates for the data access layer worries about all that stuff..
 
Top Bottom