這是使用system.data.oracleclient連接到oracle服務器後自行清理的正確方法嗎?從.Net代碼訪問Oracle DB後正確清理
using System.Data.OracleClient;
var con = new OracleConnection("some connection string");
con.Open();
con.Close();
con = null;
這是使用system.data.oracleclient連接到oracle服務器後自行清理的正確方法嗎?從.Net代碼訪問Oracle DB後正確清理
using System.Data.OracleClient;
var con = new OracleConnection("some connection string");
con.Open();
con.Close();
con = null;
OracleConnection
implements IDisposable
所以一個好主意是採用using
構造。即
using(var connection = new OracleConnection("connection string")) {
// do stuff
}
這將確保即使在出現錯誤的情況下連接也會關閉並處理。我沒有看到任何理由去否定參考。它將超出範圍並將被清理,因此不需要與IMO混淆代碼。
是的。
您可能還想在Close
之後致電con.Dispose()
。這可以確保連接立即釋放,而無需等待.NET垃圾收集器。
還記得在閱讀數據後關閉您使用的任何閱讀器。
事實上,Dispose()爲你調用Close()(從反射鏡看它)。所以你只需要調用Dispose(),或者在使用{...}塊中包裝OracleConnection – thecoop 2009-09-23 10:14:32