【问题标题】:Connection was not closed. The connection's current state is open连接未关闭。连接的当前状态为打开
【发布时间】:2016-01-07 18:47:14
【问题描述】:

如何解决这个问题?不知道为什么我已经添加了Clode()会出现这个错误。

错误会出现在这里:

public void existType()
    {
        try
        {
            con.Open();
            string existquery = "SELECT*FROM tblRoomType WHERE Type = '" + txtRoomType.Text + "'";
            da = new OleDbDataAdapter(existquery, con);
            da.Fill(ds, "tblRoomType");
            int counter = 0;
            string tabletype = "tblRoomType";
            if (counter < ds.Tables[tabletype].Rows.Count)
            {
                string type = ds.Tables[tabletype].Rows[counter]["Type"].ToString();
                if (type == txtRoomType.Text)
                {
                    editRoomType(); //There will be no error if I remove this and include the MessageBox below.
                    MessageBox.Show("Successfully Edited", "SUCCESS", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    //MessageBox.Show("This " + txtRoomType.Text + " is already exist.", "EXIST", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                else //Remove this will remove the error
                {
                    addRoomType();
                    MessageBox.Show("Successfully Added", "SUCCESS", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }//to here
            }
            con.Close();
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
    }

这是editRoomType()addRoomType()的代码:

public void addRoomType()
    {
        con.Open();
        string addtypequery = "INSERT INTO tblRoomType VALUES ('" + this.typeid + "','" + txtRoomType.Text + "','" + txtRoomRate.Text + "','" + txtExtraCharge.Text + "','" + txtCancelFee.Text + "','" + txtMaxOcc.Text + "')";
        cmd = new OleDbCommand(addtypequery, con);
        cmd.ExecuteNonQuery();
        con.Close();

        loadRoomType();
        txtRoomType.ReadOnly = false;
        txtRoomType.Enabled = false;
        txtRoomRate.Enabled = false;
        txtMaxOcc.Enabled = false;
        txtExtraCharge.Enabled = false;
        txtCancelFee.Enabled = false;
        btnAddRoomType.Enabled = false;
        txtRoomType.Clear();
        txtRoomRate.Clear();
        txtMaxOcc.Clear();
        txtExtraCharge.Clear();
        txtCancelFee.Clear();
    }

public void editRoomType()
    {
        con.Open();
        string edittypequery = "UPDATE tblRoomType SET Rate = '" + txtRoomRate.Text + "', ExtraCharge = '" + txtExtraCharge.Text + "', CancelFee = '" + txtCancelFee.Text + "', MaxOcc = '" + txtMaxOcc.Text + "' WHERE TypeID = '" + txtRoomType.Text + "'";
        cmd = new OleDbCommand(edittypequery, con);
        cmd.ExecuteNonQuery();
        con.Close();
    }

existType() 将放在这里:

private void btnAddRoomType_Click(object sender, EventArgs e)
    {
        if (txtRoomType.Text != "" || txtRoomRate.Text != "" || txtExtraCharge.Text != "" || txtCancelFee.Text != "" || txtMaxOcc.Text != "")
        {
            existType();
        }
        else
        {
            MessageBox.Show("Please fill in the space provided","OOPPSS!!", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }
    }

【问题讨论】:

    标签: c# winforms error-handling


    【解决方案1】:

    错误是因为你在existType中调用了con.Open,然后你要么调用addRoomType要么调用editRoomType,两者都调用con.Open再次之前强> 你在existType 中调用con.Close。在已经打开的连接上调用con.Open 会抛出您看到的错误。

    您可以删除对addRoomTypeeditRoomType 内的con.Open/con.Close 的调用,并且只在existType 中调用它们,或者对每种方法使用本地连接,如下所示:

    public void existType()
    {
        try
        {
            using (var conn = new SqlConnection())
            {
                conn.Open();
                string existquery = "SELECT*FROM tblRoomType WHERE Type = '" + txtRoomType.Text + "'";
                da = new OleDbDataAdapter(existquery, conn);
                da.Fill(ds, "tblRoomType");
            }
            //rest of code
        }
    }
    
    public void editRoomType()
    {
        using (var conn = new SqlConnection())
        {
            conn.Open();
            string edittypequery = "UPDATE tblRoomType SET Rate = '" + txtRoomRate.Text + "', ExtraCharge = '" + txtExtraCharge.Text + "', CancelFee = '" + txtCancelFee.Text + "', MaxOcc = '" + txtMaxOcc.Text + "' WHERE TypeID = '" + txtRoomType.Text + "'";
            cmd = new OleDbCommand(edittypequery, conn);
            cmd.ExecuteNonQuery();
        }
    }
    

    请注意,您不必调用 Close,因为 using 语句会为您执行此操作。

    【讨论】:

      【解决方案2】:

      试试这样 -

      try
          {
               con.Open();
               .......
               con.Close();
          }
          catch (Exception ex)
          {
               MessageBox.Show(ex.Message);
          }
          finally
          {
               if (con.State == ConnectionState.Open)
               {
                    con.Open(); 
               }
          }
      

      【讨论】:

      • 你最好使用using语句
      • 我只是根据他的代码回答的。他已经在没有“使用”语句的情况下格式化了他的代码。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-06-18
      • 1970-01-01
      • 1970-01-01
      • 2014-05-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多