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.
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.
Srinivasan RamamoorthiPosted Nov 2, 2021, 12:29 PM
Vinitha TPosted Nov 2, 2021, 12:16 PM
Amit Kumar SinghPosted Dec 18, 2016, 2:02 PM
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.Khaja MoizuddinPosted Dec 18, 2016, 12:09 PM
Salman BegPosted Dec 18, 2016, 12:03 PM