posted on Friday, February 06, 2004 7:22 AM
by
demiliani
ADO.NET: Close() and Dispose()
On some documentation about ADO.NET, I've found that, during the close of a SqlConnection, is a "best practice" to close a connection with these lines of code:
connection.Close();
connection.Dispose();
Why this? Are really necessary Close() and Dispose()? They both close the connection...
The Dispose() method for SqlConnection however closes the connection and also does some extra cleanup because it checks the ConnectionState... if it's closed, it sets itself to null, if its open, it calls the Close() method. After that it calls System.ComponentModel.Component.Dispose().
I'd like to know if it's really necessary to close a SqlConnection always with this 2 lines of code...