C# 我怎么可能知道SQLcommand是否被正确处理?
我怎么可能知道SQLcommand是否被正确处理 我的问题是,我的代码总是返回false,因为我的sqlcommand不等于null 我怎样才能解决这个问题 这是我的密码:C# 我怎么可能知道SQLcommand是否被正确处理?,c#,dispose,sqlcommand,C#,Dispose,Sqlcommand,我怎么可能知道SQLcommand是否被正确处理 我的问题是,我的代码总是返回false,因为我的sqlcommand不等于null 我怎样才能解决这个问题 这是我的密码: private SqlCommand Command() { cmd = new SqlCommand(QueryStr, Connection); cmd.StatementCompleted += new StatementCompletedEventHandler(cmd_Sta
private SqlCommand Command()
{
cmd = new SqlCommand(QueryStr, Connection);
cmd.StatementCompleted += new StatementCompletedEventHandler(cmd_StatementCompleted);
return cmd;
}
private void cmd_StatementCompleted(object sender, StatementCompletedEventArgs e)
{
((SqlCommand)sender).Dispose();
}
public object GetScalarResult()
{
Command();
cmd.CommandType = CommandType;
con.Open();
return cmd.ExecuteScalar();
}
public bool IsDisposedChecker()
{
if (cmd == null)
{
return true;
}
else
{
return false;
}
}
My IsDisposedChecker函数始终返回false,这意味着sqlcommand未正确处理?Dispose()
未将指针重置为null。因此ISDisposedChecker函数不能以这种方式工作
Dispose不是析构函数
我不会实现dispose检查器,如果希望返回SqlCommand作为结果,我会将dispose责任交给调用方方法
private SqlCommand Command()
{
cmd = new SqlCommand(QueryStr, Connection);
return cmd;
}
private void Test()
{
// a using block is very safe, it will dispose the command
// even when exceptions are thrown.
using(SqlCommand command = Command())
{
// do your thing....
}
}
Dispose()
不会将指针重置为null。因此,disposedChecker函数不能以这种方式工作。。请参见下面的内容。我应该使用@JeroenvanLangen来代替null吗?另外,在语句complete上进行处理看起来有点愚蠢。命令完成后,您不想处理命令的结果吗?我认为在您的代码中,StatementCompleted不是必需的。。因为用户在此仅处理对象