ADO.Net
How can I force the connection object to close after my datareader is closed??
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Jignesh TrivediPosted Jan 18, 2012, 10:31 PM
using(var myConnection = new SqlConnection(connectionstring))
{
myConnection.Open();
//your code here
myConnection.Close();
}
using is dispose your connection object after connetion close.
hope this help.
Krishna GaradPosted Jan 18, 2012, 7:59 AM
try
{
SqlConnection cn=new SqlConnection(...);
cn.Open();
//work here
}
catch(Exception e)
{}
finally
{
cn.Close();
}
in finally block any how connection got close if it is open.