我注意到SQLiteConnection
對象System.Data.SQLite
擁有兩個類似的方法:connection.Close()和connection.Dispose()之間有什麼區別?
Close()
Dispose()
同爲SQLiteDataReader
對象。
有什麼區別?
我注意到SQLiteConnection
對象System.Data.SQLite
擁有兩個類似的方法:connection.Close()和connection.Dispose()之間有什麼區別?
Close()
Dispose()
同爲SQLiteDataReader
對象。
有什麼區別?
Dispose
如果還未關閉,也會關閉連接,但在撥打Close
時,您可以重新打開連接。這是不可能的,當連接處置。
在一般情況下,不叫Close
,而只是調用dispose隱含通過包裝在using
塊連接的創建:
using (var connection = new SqlConnection(...))
{
// use connection here.
} // connection gets closed and disposed here.
的Connection.close()將簡單地關閉與服務器的連接如連接字符串中所定義。在此之後,連接可以使用/重新打開。
Connection.Dispose()
將完全清理,刪除所有非託管資源,防止再次使用該連接。一旦處置被調用,您不應該再嘗試使用該對象。在Dispose(),
內關閉()`也都會被調用。
我會建議使用using
語法,像這樣如果可能的話,以確保一切正確清理:
using(SqlLiteConnection conn = new SqlLiteConnection(...))
{
// Do work here
}
這會自動配置爲你的連接,而不考慮的異常被拋出。
也許這有助於:http://stackoverflow.com/questions/61092/close-and-dispose-which-to-call – Micha
謝謝,我沒有看到這篇文章! – Epoc