连接未关闭。连接的当前状态是开放的

时间:2015-10-09 18:12:09

标签: c# winforms error-handling

如何解决这个问题?我已经添加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);
        }
    }

2 个答案:

答案 0 :(得分:1)

错误是因为您在ConfigureAwait(false)中拨打了con.Open,然后拨打了existTypeaddRoomType,两者都再次呼叫editRoomType 之前,您在con.Open中致电con.Close。在已打开的连接上调用existType会抛出您看到的错误。

您可以删除con.Opencon.Open内的con.Close / addRoomType来电,只能在editRoomType中拨打电话,或使用本地连接对于每种方法,如下:

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声明会为您执行此操作。

答案 1 :(得分:0)

试试这个 -

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