【发布时间】:2012-06-18 16:53:56
【问题描述】:
为什么这段代码会抛出无效操作异常?
private SqlCommand cmd; // initialized in the class constructor
public void End(string spSendEventNotificationEmail) {
try {
cmd.CommandText = spSendEventNotificationEmail;
cmd.Parameters.Clear();
cmd.Parameters.Add("@packetID", SqlDbType.Int).Value = _packetID;
cmd.Parameters.Add("@statusID", SqlDbType.Int).Value = _statusID;
cmd.Parameters.Add("@website", SqlDbType.NVarChar, 100).Value = Tools.NextStep;
cmd.Connection.Open();
cmd.ExecuteNonQuery();
} finally {
cmd.Connection.Close();
cmd.Parameters.Clear();
cmd.Dispose();
}
endCall = true;
}
【问题讨论】:
-
也许你以前打开过连接?
-
我认为问题的根源在于类构造函数初始化的SqlCommand实例。在你的所有代码中都有这个 var 很容易被滥用,并导致代码的其他部分出现令人讨厌的错误
标签: c# visual-studio-2010 tsql