亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

ExecuteScalar 需要一個開放且可用的連接。連接的當前狀態是關閉的

ExecuteScalar 需要一個開放且可用的連接。連接的當前狀態是關閉的

C#
慕沐林林 2022-12-24 10:02:26
當嘗試通過 LocalDB 在我的程序中注冊一個新帳戶時,我收到一條錯誤消息:ExecuteNonQuery 需要一個打開且可用的連接。連接的當前狀態是關閉的但如您所見,我已經打開了連接我已經嘗試在多個地方設置連接字符串但無濟于事// The  methods whh will be called if the user tries to log in or register        private void LoginRegisterClick(object sender, EventArgs e)        {            // The following lines of code will execute if the user tries to register            if (lblView.Text == "Register")            {                // If all the textboxes have passed validation, the details from the textboxes are retrieved and stored                if (ucRegister1.AllFieldsValidated() == true)                {                    string[] details = ucRegister1.ReturnRegisterDetails();                    // (cmdCountUsernames) Checks to see if new username is unique                    Helper.ConnectionToDB().Open();                    SqlCommand cmdCU = new SqlCommand(@"SELECT COUNT(*) FROM LoginsTable WHERE Login = '" + details[6] + "'", Helper.ConnectionToDB());                    try                    {                        int count = (int)cmdCU.ExecuteScalar();                        //int count = Convert.ToInt32(cmdCU.ExecuteScalar());                        // If the new username is unique, the record is added into MainParentTable                        if (count == 0)                        {                            try                            {                                // Order of the details:                                         // details[(0)Firstname, (1)Surname, (2)DOB, (3)HomeTel, (4)MobileTel, (5)Address, (6)Username, (7)Password, (8)Email]                            }                            catch (Exception msg2)                            {                                MessageBox.Show("Error Message 2: " + msg2);                            }                        }                    }應根據查詢結果將計數設置為 0 或 1
查看完整描述

1 回答

?
RISEBY

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 將被關閉并釋放資源。


查看完整回答
反對 回復 2022-12-24
  • 1 回答
  • 0 關注
  • 113 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號