ADO.Net
How can I force the connection object to close after my datareader is closed??
2 Answers
Know the answer? Post it. Somebody with the same question will find it here.
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Krishna GaradRank #191 · 12.8K reputationPosted Jan 18, 2012, 2:59 AM EST
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.
Jignesh TrivediRank #16 · 50.3K reputationPosted Jan 18, 2012, 5:31 PM EST
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.