Answer Description: As per the standard guideline connection object should be dispose in any case so that dangling connection object should not lie in memory. Connection object is handling SQL server connection which can not be disposing by .NET garbage collector.
To achieve this behavior we should always use try finally block which handling connection object and make sure to calling dispose method of connection object into finally block.
System.Data.SqlClient.SqlConnection connection =
new System.Data.SqlClient.SqlConnection();
try
{
connection.Open();
//do some work
}
finally
{
connection.Dispose();
}
There is a “using” statement in .net framework which is a shortcut of try finally block.
using ( System.Data.SqlClient.SqlConnection connection =
new System.Data.SqlClient.SqlConnection() )
{
connection.Open();
//do some work
//no need to call connection.Dispose()
}