如果 .NET SqlConnection 在 `using` 语句中抛出异常,我是否需要手动关闭它?

Do I need to manually close a .NET SqlConnection, if it throws an exception inside a `using` statement?

如果 SqlConnectionusing 语句中执行期间抛出异常,我是否需要在 finally 中手动关闭连接?还是 using 语句的范围会为我调用 Dispose 方法(在 SqlConnection 上)...因此会为我(自动)执行 .Close(); 方法?

例如:

using (var sqlConnection = new SqlConnection(_connectionString)
{
   sqlConnection.Open();

   throw new Exception("boom!");
}

using (var sqlConnection = new SqlConnection(_connectionString)
{
    try
    {
        sqlConnection.Open();

        throw new Exception("boom!");
    }
    finally
    {
        sqlConection.Close();
    }
}

此外,是否将其包装在 TransactionScope + 抛出异常,影响我应该如何 .Close()using 范围自动为我执行此操作。

否,如果在using

范围内仍会被处理

The using statement ensures that Dispose is called even if an exception occurs while you are calling methods on the object. You can achieve the same result by putting the object inside a try block and then calling Dispose in a finally block; in fact, this is how the using statement is translated by the compiler.

Source : MSDN.