在下面的示例中,如果异常在using
语句中,当抛出异常时,连接是否将关闭并释放?
using (var conn = new SqlConnection("..."))
{
conn.Open();
// stuff happens here and exception is thrown...
}
我知道下面的代码将确保它能做到,但我很好奇使用语句是如何做到的.
var conn;
try
{
conn = new SqlConnection("...");
conn.Open();
// stuff happens here and exception is thrown...
}
// catch it or let it bubble up
finally
{
conn.Dispose();
}