Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

using SqlConnection.Open inside using () {} block?

Do I need to use SqlConnection.Open() inside:

using (SqlConnection var = new SqlConnection()) 
{
   //my operations
}

?

What happens if I don't dispose this connection by SqlConnection.Close() function?

like image 897
Karthik Nishanth Avatar asked Dec 03 '22 18:12

Karthik Nishanth


1 Answers

Yes, you need to Open your connection inside the using block.

If you don't explicitly Close() the connection, the end of the using block will do it for you as the using disposes of the connection. When a connection is disposed it is automatically closed.

like image 192
Colin Mackay Avatar answered Dec 25 '22 05:12

Colin Mackay