Why do we use using keyword while establishing connection in C#.?
Ex- using(connection con=new connection())
{
// some ado .net code to access or insert/data .
}
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
Amit Kumar SinghRank #562 · 4.3K reputationPosted Dec 18, 2016, 9:02 AM EST
usingwill take care of it for you. Under the hood,SqlConnection.Dispose()calls theSqlConnection.Close()method, andSqlCommand.Dispose()callsSqlCommand.Close()usingstatement is syntactic sugar for atry ... finallythat disposes theIDisposableobject in thefinally.Salman BegRank #284 · 8.8K reputationPosted Dec 18, 2016, 7:03 AM EST
Khaja MoizuddinRank #270 · 9.4K reputationPosted Dec 18, 2016, 7:09 AM EST
Vinitha TRank #8,005 · 230 reputationPosted Nov 2, 2021, 8:16 AM EDT
Srinivasan RamamoorthiRank #877 · 2.5K reputationPosted Nov 2, 2021, 8:29 AM EDT