我正在研究一直到今天爲止的項目。但是現在,當我運行它,它經歷了幾個不同的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()函數中打開連接,但從未在程序中關閉。 –