I read this question on relating to disposing of SQL connections.
My question is, how bad is it to simply close a sql connection, but not dispose of it?
We have a function which is simply closed, but never disposed, and it is used 1000s times a day.
Is it better to simply close it, or would it be better to close and dispose of it?
I am aware that dispose() also closes the connection, however I would like to know why close doesn’t dispose of the connection.
The important thing about connections is to close them so they are returned to the connection pool.
As such, there is little difference between disposing and closing a connection, so long as you are disciplined about closing and not reusing connections.
However, being in the habit of wrapping the creation of a connection in a
usingstatement means you never forget to close it.It is a general good idiom to follow – creation of any object that implements
IDisposableshould be wrapped in ausingstatement, and as such an idiom it is a good one to follow with connections as well.