代码之家  ›  专栏  ›  技术社区  ›  Johnny Bones

连接未关闭。连接的当前状态为“打开”

  •  1
  • Johnny Bones  · 技术社区  · 8 年前

    我遇到了这个错误。我读过一些帖子,上面说要使用Using块,但我有一个,而且我仍然有错误。

        protected void btnEdit_OnClick(object sender, EventArgs e)
        {
            MdlCommentsExtender.Enabled = true;
            MdlCommentsExtender.Show();
            Button button = (Button)sender;
            string buttonId = button.ID;
            string[] tokens = buttonId.Split('-');
            ScriptManager.GetCurrent(this).SetFocus(this.txtCommentBox);
    
            //**************************/
             try
             {
    
                 using (SqlConnection conn = new SqlConnection())
                 {
                     conn.ConnectionString = ConfigurationManager.ConnectionStrings["ConnCST"].ToString();
                     conn.Open();
                     SqlCommand cmd2 = new SqlCommand();
                     cmd2.Connection = conn;
                     string CmdTxt = "Select CBL.ID, CBL.[Category], CBL.[Provision], CTL.MarkForReview, CTL.IssueType, CTL.Resolution, CTL.Feedback, CTL.TemplateID";
                     CmdTxt = CmdTxt + " from [tblCSTBenefitList] CBL";
                     CmdTxt = CmdTxt + " LEFT JOIN tblCSTTemplateList CTL";
                     CmdTxt = CmdTxt + " ON CBL.ID = CTL.BenefitID";
                     CmdTxt = CmdTxt + " where CBL.ID > '0'";
                     CmdTxt = CmdTxt + " ORDER BY CBL.[Category], CBL.[Provision] ASC";
    
                     cmd2.CommandText = CmdTxt;
                     SqlDataReader reader;
                     conn.Open();
                     reader = cmd2.ExecuteReader();
                     reader.Read();
    
                     lblBenCatX.Text = Convert.ToString(reader["Category"]);
                     lblBenProvX.Text = Convert.ToString(reader["Provision"]);
                     txtCommentBox.Text = Convert.ToString(reader["Feedback"]);
                 }
             }
             catch (Exception ex) 
             { 
                 Response.Write(ex.Message); 
             }
    
            /*******************************************/
    
            //txtCommentBox.Text = "First: " + tokens[0] + " and then " + tokens[1] + "";
        }
    

    它发生在 conn.Open(); 线有什么想法吗?

    1 回复  |  直到 8 年前
        1
  •  6
  •   bassfader    8 年前

    您尝试打开连接两次,您有 conn.Open() 在你的方法中有2次,但你在尝试“重新打开”连接之前,还是先关闭了连接。

    是否确实要打开连接两次?尝试删除第二个 ,这实际上应该有效。