1 回答
TA貢獻1856條經驗 獲得超5個贊
問題是由這一行(以及以下命令中的類似行)引起的
SqlCommand cmdCU = new SqlCommand(........, Helper.ConnectionToDB());
我敢打賭,每次調用Helper.ConnectionToDB 時都會創建 SqlConnection 對象的新實例。
現在,即使你有這條線
Helper.ConnectionToDB().Open();
您正在打開一個連接實例,但是由于該命令再次調用Helper.ConnectionToDB,您在每個命令中獲得了一個不同的實例并在仍然關閉時使用它。
你需要一種完全不同的方法
.... previous stuff....
if (ucRegister1.AllFieldsValidated() == true)
{
string[] details = ucRegister1.ReturnRegisterDetails();
using(SqlConnection cnn = Helper.ConnectionToDB())
{
cnn.Open();
SqlCommand cmdCU = new SqlCommand("......", cnn);
.... replace all calls to Helper.ConnectionDB with the cnn variable ....
.....
} // <== This close the connection
}
using 語句可幫助您控制 SqlConnection 及其使用的資源。SqlConnection 是一個一次性對象,重要的非托管資源鏈接到它。盡快釋放這些資源非常重要,using 語句保證當代碼退出 using 塊時,SqlConnection 將被關閉并釋放資源。
- 1 回答
- 0 關注
- 113 瀏覽
添加回答
舉報
