应该将对象列表插入数据库的函数只插入其中一个对象 n 次

Function that is supposed to insert a list of objects into a database only inserts one of the objects n times

我一直在尝试创建一个函数来为我库存中缺少的所有物品设置订单。

RequiredStockForAllOrders 基本上为 stockItems 中的每个对象分配一个值,这让我知道我需要订购多少商品。

我检查了一个消息框,它改变了值(包括 ID 和数量),但是当我 运行 应该插入每个产品及其各自数量的循环时,我只插入了 1 个产品 n 次其中 n 是列表中项目的数量。

    private void AddAllRequiredItems_Click(object sender, EventArgs e)
    {
        var stockItems = new List<MyData>();
        //MyData is an object with  a productID int and a productQuantity int 

        RequiredStockForAllOrders(stockItems);
        //determining the quantity required for each item

        OleDbConnection con = new OleDbConnection(DatabaseConnectionString);
        OleDbCommand cmd = new OleDbCommand();
        cmd.Connection = con;
        con.Open();

        string sql2 = "INSERT INTO restockingDetails(RestockingID,ProductID,Quantity,Shop_ID) values (@restockingID,@productID,@quantity,@shop_id)";
        cmd.CommandText = sql2;


        int i = 0;

        while (i < stockItems.Count)
        {
            try
            {
                MessageBox.Show(stockItems[i].productId.ToString()); //For testing

                cmd.Parameters.AddWithValue("@restockingID", restockingOrder);
                cmd.Parameters.AddWithValue("@productID", stockItems[i].productId);
                cmd.Parameters.AddWithValue("@quantity", stockItems[i].productQuantity);
                cmd.Parameters.AddWithValue("@shop_id", shopIDGlobal);
                cmd.ExecuteNonQuery();
                MessageBox.Show(" Item added to list"); //for testing


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

            }


            i = i + 1;
        }

      con.Close()
    }

在添加参数之前添加这一行

MessageBox.Show(stockItems[i].productId.ToString()); //For testing
cmd.Parameters.Clear();
cmd.Parameters.AddWithValue("@restockingID", restockingOrder);

您的实际代码继续向命令集合添加参数,但查询仅使用前四个。对于其他提供程序,此代码将导致错误(参数过多),但 OleDb 在这一点上有些限制。可能是因为它不按名称识别参数,而是按位置识别参数

更好的方法是只定义一次参数,然后在循环内更新它们的值

private void AddAllRequiredItems_Click(object sender, EventArgs e)
{
    var stockItems = new List<MyData>();

    RequiredStockForAllOrders(stockItems);

    string sql2 = "INSERT INTO restockingDetails(RestockingID,ProductID,Quantity,Shop_ID) values (@restockingID,@productID,@quantity,@shop_id)";

    using(OleDbConnection con = new OleDbConnection(DatabaseConnectionString))
    using(OleDbCommand cmd = new OleDbCommand(sql2, con))
    {
        con.Open();
        cmd.Parameters.Add("@restockingID", OleDbType.Integer);
        cmd.Parameters.Add("@productID", OleDbType.Integer);
        cmd.Parameters.Add("@quantity", OleDbType.Integer);
        cmd.Parameters.Add("@shop_id", OleDbType.Integer);
        foreach(MyData item in stockItems)
        {
           try
           {
               cmd.Parameters["@restockingID"].Value = restockingOrder;
               cmd.Parameters["@productID"].Value = item.productId;
               cmd.Parameters["@quantity"].Value = item.productQuantity;
               cmd.Parameters["@shop_id"].Value = shopIDGlobal;
               cmd.ExecuteNonQuery();
           }
           catch (Exception ex)
           {
               MessageBox.Show(ex.Message);
           }
      }
   }
}

在 while 循环中创建命令:

    OleDbConnection con = new OleDbConnection(DatabaseConnectionString);
    OleDbCommand cmd;

    string sql2 = "INSERT INTO restockingDetails(RestockingID,ProductID,Quantity,Shop_ID) values (@restockingID,@productID,@quantity,@shop_id)";

    int i = 0;

    while (i < stockItems.Count)
    {
        try
        {
            MessageBox.Show(stockItems[i].productId.ToString()); //For testing

            cmd = new OleDbCommand();
            cmd.Connection = con;
            con.Open();
            cmd.CommandText = sql2;
            cmd.Parameters.AddWithValue("@restockingID", restockingOrder);
            cmd.Parameters.AddWithValue("@productID", stockItems[i].productId);
            cmd.Parameters.AddWithValue("@quantity", stockItems[i].productQuantity);
            cmd.Parameters.AddWithValue("@shop_id", shopIDGlobal);
            cmd.ExecuteNonQuery();
            MessageBox.Show(" Item added to list"); //for testing
            con.Close()

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

        }


        i = i + 1;
    }