2017-07-16 66 views

回答

1

究竟是什麼讓你感到不滿意?在代碼中換行是可以的,除了可以將它放入using語句以確保即使發生錯誤時也能處理它,異常:

using (var sqlconn = new ...) 
using (var sqlcom = new ...) 
{ 
    sqlcon.open(); 
    sqlcom.ExecuteNonQuery(); 
} 

這樣Dispose(其會自動調用Close)被稱爲離開using時,無論是在通常的方式或通過一個例外。

2

using會照顧它。在引擎蓋下,SqlConnection.Dispose()調用SqlConnection.Close()方法,並且SqlCommand.Dispose()調用SqlCommand.Close()

作爲附加背景,using statementtry ... finally的語法糖,它將IDisposable對象置於finally中。

相關問題