我正在瀏覽ASP.NET應用程序中的一些舊的C#.NET代碼,確保使用塊確保所有SqlConnections都封裝在中。如果我在SqlConnection的using語句內部捕獲異常,那麼使用仍然處理關閉和處置我的連接?
這段代碼用於打開CN和噠和關閉以及在兩個捕獲塊,並且該方法的結束處理它們。我加入了usings並不能找出某些如果使用塊還是處理連接的配置,如果有異常的拋出嘗試和夾在抓。 This question似乎表明它確實如此。
using (SqlConnection cn = new SqlConnection(Global.sDSN))
{
using (SqlDataAdapter da = new SqlDataAdapter())
{
// do some stuff
try
{
// do stuff that might throw exceptions
}
catch (catch (System.Exception e)
{
//return something here
// can I ditch these because the using's handle it?
da.Dispose();
cn.Close();
cn.Dispose();
return msg;
}
}
}