我们从VisualStudio2010中的代码分析中得到以下警告,我想知道这是一个误报,我们可以安全地忽略它,还是应该重构代码以正确地处理对象。
相关代码:
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。用法:可以释放对象“oraConnection”
在方法“ClassName.MyFunction()”中多次使用。避免产生
System.ObjectDisposedException不应调用多个Dispose
物体上的时间。