【问题标题】:Function that is supposed to insert a list of objects into a database only inserts one of the objects n times应该将对象列表插入数据库的函数仅将对象之一插入 n 次
【发布时间】:2019-12-06 07:31:43
【问题描述】:

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

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()
    }

【问题讨论】:

  • 将创建命令的代码移到while循环中,我认为这就是问题所在。你不断重复使用相同的命令。

标签: c# database loops insert oledb


【解决方案1】:

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

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);
           }
      }
   }
}

【讨论】:

    【解决方案2】:

    在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;
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-01-24
      • 1970-01-01
      • 2019-09-29
      • 2014-10-10
      • 1970-01-01
      • 1970-01-01
      • 2013-01-19
      相关资源
      最近更新 更多