我正在研究一直到今天为止的项目。但是现在,当我运行它,它经历了几个不同的Stored Procedure
调用它与消息The connection was not closed. The connection's current state is open.
SQL连接打开异常
,我感到我可以把在一个检查,看看是否连接已经打开抛出InvalidOperationException
,但是这个代码不是招没有改变(它在Version Control
下,没有修改),所以我正在寻找其他潜在的解释。
SQL中没有被释放的锁吗?是否有一个我应该注意并杀死的过程?
我不能真的发布代码,因为它有很多它,它被分成更小的方法,这使得更难以拉出单个项目。例如:
public SqlConnection Connection
{
get
{
this._connection.Open();
return _connection;
}
}
public IDataReader RetrieveRecord(int Id)
{
//SP
SqlCommand cmd = this.Connection.CreateCommand();
cmd.CommandText = "SelectRecord";
cmd.CommandType = CommandType.StoredProcedure;
//Parameters
cmd.Parameters.Add(new SqlParameter("@tID", Id));
//instruct the data reader to close its connection when its Close method is called by passing the CommandBehavior.CloseConnection
return cmd.ExecuteReader(CommandBehavior.CloseConnection);
}
没什么大问题,我只是不明白为什么这个连接现在抛出一个异常。
您正在公开SqlConnection Connection()函数中打开连接,但从未在程序中关闭。 –