连接未关闭。连接的当前状态已打开。 [英] The connection was not closed.the connection's current state is open.

查看:117
本文介绍了连接未关闭。连接的当前状态已打开。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不打算在这种情况下做什么?

可以帮助我吗?



我尝试了什么:



I am not geeting what do in this case?
can any plz help me?

What I have tried:

private void button1_Click(object sender, EventArgs e)
        {
            using (OleDbConnection con1 = new OleDbConnection(con))
            {
                OleDbCommand cmd = new OleDbCommand();
                cmd.Connection = con1;
                OleDbDataAdapter da;

                DataSet ds = new DataSet();

                if ((textBox4.Text == string.Empty) || (textBox5.Text == string.Empty) || (comboBox1.Text == string.Empty) || (comboBox2.Text == string.Empty))
                {

                    MessageBox.Show("Please enter Your All the Details in the Related Fields ...!!!!!", "Message", MessageBoxButtons.OK, MessageBoxIcon.Warning);


                }
                else
                {

                    cmd = new OleDbCommand("SELECT * from Spldetails where Date = @d1 and EmpId = @v1 and Splwrk = @c1 and Splwrkdetails = @sd", con1);
                    con1.Open();
                    da = new OleDbDataAdapter(cmd);
                    da.SelectCommand.Parameters.Add("@v1", OleDbType.Variant, 30).Value = this.textBox1.Text;
                    da.SelectCommand.Parameters.Add("@d1", OleDbType.Date).Value = this.dateTimePicker1.Text;
                    da.SelectCommand.Parameters.Add("@c1", OleDbType.Variant, 100).Value = this.comboBox1.Text;
                    da.SelectCommand.Parameters.Add("@sd", OleDbType.Variant, 300).Value = this.textBox6.Text;
                    da.Fill(ds);
                    int i = ds.Tables[0].Rows.Count;
                    if (i > 0)
                    {
                        MessageBox.Show("Duplicate Details !!!! You have already Submitted your details on " + dateTimePicker1.Text + ".");
                        //ds.Clear();
                        textBox4.Text = "";
                        textBox5.Text = "";
                        textBox6.Text = "";
                        comboBox1.Text = "";
                        comboBox2.Text = "";
                        con1.Close();

                    }
                    else
                    {

                        try
                        {

                            
                            string query1 = "INSERT INTO Spldetails([Date],[EmpId],[EmpName],[Designation],[Records],[Splwrkhrs],[Team],[Splwrk],[Splwrkdetails],[Allottedby]) VALUES(@d1,@v1,@v2,@dg,@rd,@shr,@tm,@sw,@sd,@ab)";
                            cmd = new OleDbCommand(query1, con1);
                            con1.Open();
                            da = new OleDbDataAdapter(cmd);
                            da.SelectCommand.Parameters.Add("@v1", OleDbType.Variant, 30).Value = this.textBox1.Text;
                            da.SelectCommand.Parameters.Add("@v2", OleDbType.Variant, 100).Value = this.textBox2.Text;
                            da.SelectCommand.Parameters.Add("@dg", OleDbType.Variant, 50).Value = this.textBox3.Text;
                            da.SelectCommand.Parameters.Add("@rd", OleDbType.Variant, 15).Value = Convert.ToDouble(this.textBox4.Text);
                            da.SelectCommand.Parameters.Add("@shr", OleDbType.Variant, 15).Value = Convert.ToDouble(this.textBox5.Text);
                            da.SelectCommand.Parameters.Add("@tm", OleDbType.Variant, 100).Value = this.textBox8.Text;
                            da.SelectCommand.Parameters.Add("@d1", OleDbType.Date).Value = this.dateTimePicker1.Text;
                            da.SelectCommand.Parameters.Add("@ab", OleDbType.Variant, 100).Value = this.comboBox1.Text;
                            da.SelectCommand.Parameters.Add("@sw", OleDbType.Variant, 100).Value = this.comboBox2.Text;
                            da.SelectCommand.Parameters.Add("@sd", OleDbType.Variant, 300).Value = this.textBox6.Text;
                            cmd.ExecuteNonQuery();
                            con1.Close();

                            MessageBox.Show("Congratulations....Your Details have been submitted successfully");

                            textBox4.Text = "";
                            textBox5.Text = "";
                            textBox6.Text = "";
                            comboBox1.Text = "";
                            comboBox2.Text = "";


                            
                        }

                        catch (Exception exp)
                        {
                            MessageBox.Show("error" + exp);
                        }

                    }
                }
              
            }
        }

推荐答案

您正在尝试打开已打开的连接:

You are trying to open an already open connection:
private void button1_Click(object sender, EventArgs e)
        {
            using (OleDbConnection con1 = new OleDbConnection(con))
            {
...
                if (...)
                {
...
                }
                else
                {
...
                    con1.Open();
...
                    if (i > 0)
                    {
...                        con1.Close();

                    }
                    else
                    {
                        try
                        {
...
                            con1.Open();
...
                            con1.Close();
...
                        }
                        catch (Exception exp)
                        {
                            MessageBox.Show("error" + exp);
                        }
                    }
                }
            }
        }

在方法的顶部进行有效性检查,如果失败则退出。

然后创建你的连接并立即打开它。

当它超出的范围时它会自动关闭阻止。

Do your validity checks at the top of the method, and exit if they fail.
Then create your connection and immediately open it.
It will be closed automatically when it goes out of scope of the using block.


这篇关于连接未关闭。连接的当前状态已打开。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆