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?
The using statement is just syntactic sugar which ensures that Dispose gets called on the IDisposable instance no matter what, so in your case using (SqlConnection connection = new SqlConnection()) is roughly equivalent to (as per MSDN):
SqlConnection connection = new SqlConnection();
try
{
// Operations.
}
finally
{
if (connection != null)
{
connection.Dispose();
}
}
In the case of SqlConnection Dispose and Close calls are actually equivalent, so a call to Close would be redundant within a using (SqlConnection) block.
Calling Open, on the other hand, will still be necessary in many scenarios (before a call to ExecuteReader, ExecuteScalar, ExecuteNonQuery, for example). The using statement has no effect on whether or not you need to explicitly open the connection - only on whether or not you need to explicitly close it.