0
我們從Visual Studio 2010中的代碼分析中得到以下警告,並且我想知道這是否是我們可以安全忽略的錯誤肯定或者應該重構代碼以正確處置目的。CA2202來自OracleConnection處置代碼分析的警告
相關的代碼:
public void MyFunction()
{
OracleConnection oraConnection = null;
OracleCommand oraCommand = null;
try
{
// Connect to the database
oraConnection = new OracleConnection(connectionString);
oraConnection.Open();
// Prepare and run the query
oraCommand = new OracleCommand(sqlQuery, oraConnection);
oraCommand.ExecuteNonQuery();
}
catch { throw; }
finally
{
// Perform a safe cleanup
if (oraCommand != null) { oraCommand.Dispose(); }
if (oraConnection != null)
{
oraConnection.Close();
oraConnection.Dispose();
}
}
}
相關錯誤消息:
警告18 CA2202:Microsoft.Usage:對象 'oraConnection' 可以被設置成比一次 更方法「類名。 MyFunction的()」。爲了避免產生一個 System.ObjectDisposedException,你不應該在一個對象上調用Dispose多於一個的 時間。
我在ODP.NET的Oracle文檔中找到了它,並指出OracleConnection.Dispose()調用Close方法,因此它看起來像準則將被更新爲只調用Dispose方法。 – rjzii 2010-06-28 13:15:14