Strictly speaking, the SqlConnection object is just a managed object, so
it's no big deal by itself. However, one of it's internal objects manages
native resources (network connections, native buffers, etc.) which need to
be carefully tracked to make sure we don't leak them. That's why is so
important to call Close or Dispose on the connection, so the internal
objects are properly cleaned-up.
Using CommandBehavior.CloseConnection is a great option if you know that you
won't need the connection after processing the result-set. Keep in mind,
though, that in this case you have to make sure you close the DataReader;
it's as important as closing the connection, because we'll close the
connection when you close the reader if CloseConnection is specified.
--
Pablo Castro
Program Manager - ADO.NET Team
Microsoft Corp.
This posting is provided "AS IS" with no warranties, and confers no rights.
Daniel Billingsley said:
The unmanaged resource we're concerned about *is* the literal connection
though, right? By that I mean that any differences between Close and
Dispose are somewhat moot for practical resource management concerns, isn't
it?
Also, what if you just rely on the following code to close the connection?
Then technically you don't need to explicitly close or dispose the
connection object itself, right?
.ExecuteReader(CommandBehavior.CloseConnection)