错误:ExecuteNonQuery需要一个开放且可用的连接

时间:2016-08-22 03:32:26

标签: c# ms-access

我正在创建一个系统,允许用户在文本框控件中输入10个数据,然后将所有数据一起保存在数据库中。但这个错误让我有一段时间的股票,我不知道我哪里出错了。有人可以帮我吗?请善待,并提前感谢你。也可以随意编辑我的问题。

这是我的代码:

private void btnAdd_Click(object sender, EventArgs e)
{
    DateTime getdate = DateTime.Now;
    String time = getdate.ToString("F");
    try
    {
        con.Open();
        var rows = new[]
        {
            new {Item = txtItem.Text, Product = txtProduct.Text, Quantity = txtQuantity.Text},
            new {Item = txtItem2.Text, Product = txtProduct2.Text, Quantity = txtQuantity2.Text},
            new {Item = txtItem3.Text, Product = txtProduct3.Text, Quantity = txtQuantity3.Text},
            new {Item = txtItem4.Text, Product = txtProduct4.Text, Quantity = txtQuantity4.Text},
            new {Item = txtItem5.Text, Product = txtProduct5.Text, Quantity = txtQuantity5.Text},
            new {Item = txtItem6.Text, Product = txtProduct6.Text, Quantity = txtQuantity6.Text},
            new {Item = txtItem7.Text, Product = txtProduct7.Text, Quantity = txtQuantity7.Text},
            new {Item = txtItem8.Text, Product = txtProduct8.Text, Quantity = txtQuantity8.Text},
            new {Item = txtItem9.Text, Product = txtProduct9.Text, Quantity = txtQuantity9.Text},
            new {Item = txtItem10.Text, Product = txtProduct10.Text, Quantity = txtQuantity10.Text}
        };

        foreach (var row in rows)//Check if ItemCode exist
        {
            if (!String.IsNullOrEmpty(row.Item))
            {
                OleDbCommand command = new OleDbCommand(@"Select * from TblInventory where ItemCode='" + txtItem.Text + "'");
                command.Connection = con;
                command.Parameters.AddWithValue("itemcode", txtItem.Text);
                OleDbDataReader reader = command.ExecuteReader();

                if (reader.Read())
                {
                    OleDbCommand cmd = new OleDbCommand(@"Update TblInventory set Quantity = Quantity + @Quantity WHERE ItemCode = @itemcode");

                    cmd.Connection = con;
                    cmd.Parameters.AddWithValue("@Quantity", Convert.ToInt32(txtQuantity.Text));
                    txtProduct.Text = reader["ProductName"].ToString();
                    cmd.Parameters.AddWithValue("@itemcode", txtItem.Text);
                    cmd.Parameters.AddWithValue("@DateAndTime", time);
                    cmd.ExecuteNonQuery();// HERE <<<
                    MessageBox.Show("You added " + txtQuantity.Text + " " + txtProduct.Text, "Existing Item");
                }
            }
            else //Add new Data if Item Code is not exit;
            {
                var rows2 = new[]
                {
                    new {Item = txtItem.Text, Product = txtProduct.Text, Quantity = txtQuantity.Text},
                    new {Item = txtItem2.Text, Product = txtProduct2.Text, Quantity = txtQuantity2.Text},
                    new {Item = txtItem3.Text, Product = txtProduct3.Text, Quantity = txtQuantity3.Text},
                    new {Item = txtItem4.Text, Product = txtProduct4.Text, Quantity = txtQuantity4.Text},
                    new {Item = txtItem5.Text, Product = txtProduct5.Text, Quantity = txtQuantity5.Text},
                    new {Item = txtItem6.Text, Product = txtProduct6.Text, Quantity = txtQuantity6.Text},
                    new {Item = txtItem7.Text, Product = txtProduct7.Text, Quantity = txtQuantity7.Text},
                    new {Item = txtItem8.Text, Product = txtProduct8.Text, Quantity = txtQuantity8.Text},
                    new {Item = txtItem9.Text, Product = txtProduct9.Text, Quantity = txtQuantity9.Text},
                    new {Item = txtItem10.Text, Product = txtProduct10.Text, Quantity = txtQuantity10.Text}
                };

                foreach (var row2 in rows)
                {
                    if (!String.IsNullOrEmpty(row2.Item) && !String.IsNullOrEmpty(row2.Product) && !String.IsNullOrEmpty(row2.Quantity))
                    {
                        OleDbCommand cmdInsert = new OleDbCommand(
                                @"insert into TblInventory (ItemCode,ProductName,Quantity,DateAndTime)values(@ItemCode,@ProductName,@Quantity,@DateAndTime)");
                        cmdInsert.Connection = con;
                        cmdInsert.Parameters.AddWithValue("ItemCode", row2.Item);
                        cmdInsert.Parameters.AddWithValue("ProductName", row2.Product);
                        cmdInsert.Parameters.AddWithValue("Quantity", row2.Quantity.ToString());
                        cmdInsert.Parameters.AddWithValue("DateAndTime", DateTime.Now);

                        cmdInsert.ExecuteNonQuery(); HERE <<<
                        MessageBox.Show("You added " + row.Quantity + " " + row.Product, "New Item");
                    }
                }
            }
            showGrid2();
            con.Close();
            clear();
        }
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
} 

2 个答案:

答案 0 :(得分:0)

这是我第一次回答问题,所以如果我误解了某些内容,请轻松告诉我。

从我所看到的情况来看,在某个时刻cmdInsert.ExecuteNonQuery();在你的foreach循环中被称为之后的 con.Close();。 因为每次迭代都满足if子句时都会调用con.Close();

也许尝试这样的事情:

<强> editted


private void btnAdd_Click(object sender, EventArgs e)
        {
            DateTime getdate = DateTime.Now;
            String time = getdate.ToString("F");
            try
            {

                var rows = new[]
                {
                    new {Item = txtItem.Text, Product = txtProduct.Text, Quantity = txtQuantity.Text},
                    new {Item = txtItem2.Text, Product = txtProduct2.Text, Quantity = txtQuantity2.Text},
                    new {Item = txtItem3.Text, Product = txtProduct3.Text, Quantity = txtQuantity3.Text},
                    new {Item = txtItem4.Text, Product = txtProduct4.Text, Quantity = txtQuantity4.Text},
                    new {Item = txtItem5.Text, Product = txtProduct5.Text, Quantity = txtQuantity5.Text},
                    new {Item = txtItem6.Text, Product = txtProduct6.Text, Quantity = txtQuantity6.Text},
                    new {Item = txtItem7.Text, Product = txtProduct7.Text, Quantity = txtQuantity7.Text},
                    new {Item = txtItem8.Text, Product = txtProduct8.Text, Quantity = txtQuantity8.Text},
                    new {Item = txtItem9.Text, Product = txtProduct9.Text, Quantity = txtQuantity9.Text},
                    new {Item = txtItem10.Text, Product = txtProduct10.Text, Quantity = txtQuantity10.Text}
                };
                foreach (var row in rows)//Check if ItemCode exist
                {
                    con.Open();
                    if (!String.IsNullOrEmpty(row.Item))
                    {
                        OleDbCommand command = new OleDbCommand(@"Select * from TblInventory where ItemCode='" + txtItem.Text + "'");
                        command.Connection = con;
                        command.Parameters.AddWithValue("itemcode", txtItem.Text);
                        OleDbDataReader reader = command.ExecuteReader();

                        if (reader.Read())
                        {
                            OleDbCommand cmd = new OleDbCommand(@"Update TblInventory set Quantity = Quantity + @Quantity WHERE ItemCode = @itemcode");

                            cmd.Connection = con;
                            cmd.Parameters.AddWithValue("@Quantity", Convert.ToInt32(txtQuantity.Text));
                            txtProduct.Text = reader["ProductName"].ToString();
                            cmd.Parameters.AddWithValue("@itemcode", txtItem.Text);
                            cmd.Parameters.AddWithValue("@DateAndTime", time);
                            cmd.ExecuteNonQuery();// HERE
                            MessageBox.Show("You added " + txtQuantity.Text + " " + txtProduct.Text, "Existing Item");
                        }
                    }
                    else //Add new Data if Item Code is not exit;
                    {
                        var rows2 = new[]
                        {
                             new {Item = txtItem.Text, Product = txtProduct.Text, Quantity = txtQuantity.Text},
                             new {Item = txtItem2.Text, Product = txtProduct2.Text, Quantity = txtQuantity2.Text},
                             new {Item = txtItem3.Text, Product = txtProduct3.Text, Quantity = txtQuantity3.Text},
                             new {Item = txtItem4.Text, Product = txtProduct4.Text, Quantity = txtQuantity4.Text},
                             new {Item = txtItem5.Text, Product = txtProduct5.Text, Quantity = txtQuantity5.Text},
                             new {Item = txtItem6.Text, Product = txtProduct6.Text, Quantity = txtQuantity6.Text},
                             new {Item = txtItem7.Text, Product = txtProduct7.Text, Quantity = txtQuantity7.Text},
                             new {Item = txtItem8.Text, Product = txtProduct8.Text, Quantity = txtQuantity8.Text},
                             new {Item = txtItem9.Text, Product = txtProduct9.Text, Quantity = txtQuantity9.Text},
                             new {Item = txtItem10.Text, Product = txtProduct10.Text, Quantity = txtQuantity10.Text}
                        };
                        foreach (var row2 in rows)
                        {
                            if (!String.IsNullOrEmpty(row2.Item) && !String.IsNullOrEmpty(row2.Product) && !String.IsNullOrEmpty(row2.Quantity))
                            {
                                OleDbCommand cmdInsert = new OleDbCommand(
                                                 @"insert into TblInventory (ItemCode,ProductName,Quantity,DateAndTime)values(@ItemCode,@ProductName,@Quantity,@DateAndTime)");
                                cmdInsert.Connection = con;
                                cmdInsert.Parameters.AddWithValue("ItemCode", row2.Item);
                                cmdInsert.Parameters.AddWithValue("ProductName", row2.Product);
                                cmdInsert.Parameters.AddWithValue("Quantity", row2.Quantity.ToString());
                                cmdInsert.Parameters.AddWithValue("DateAndTime", DateTime.Now);

                                cmdInsert.ExecuteNonQuery(); HERE
                                MessageBox.Show("You added " + row.Quantity + " " + row.Product, "New Item");
                            }
                        }
                    }
                    showGrid2();
                    clear();
                    con.Close();
                }

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

答案 1 :(得分:0)

使用用于打开连接,它会自动处理连接的关闭。所以最好和最有效的做法是尽可能晚地打开连接并尽快关闭它。

foreach (var row in rows)//Check if ItemCode exist
                    {

                            if (!String.IsNullOrEmpty(row.Item))
                            {
                                OleDbCommand command = new OleDbCommand(@"Select * from TblInventory where ItemCode='" + txtItem.Text + "'");
                                using (command.Connection = con)
                                {
                                command.Parameters.AddWithValue("itemcode", txtItem.Text);
                                con.open;
                                OleDbDataReader reader = command.ExecuteReader();

                                if (reader.Read())
                                {
                                    OleDbCommand cmd = new OleDbCommand(@"Update TblInventory set Quantity = Quantity + @Quantity WHERE ItemCode = @itemcode");

                                    cmd.Connection = con;
                                    cmd.Parameters.AddWithValue("@Quantity", Convert.ToInt32(txtQuantity.Text));
                                    txtProduct.Text = reader["ProductName"].ToString();
                                    cmd.Parameters.AddWithValue("@itemcode", txtItem.Text);
                                    cmd.Parameters.AddWithValue("@DateAndTime", time);
                                    cmd.ExecuteNonQuery();// HERE
                                    MessageBox.Show("You added " + txtQuantity.Text + " " + txtProduct.Text, "Existing Item");
                                }
                               }

                    }
            }
    //dont use con.Close anywhere in the code .