簡體   English   中英

如何插入數據庫多個gridview選擇

[英]how to insert the database multiple gridview choises

  siparisDetayy order = new siparisDetayy();
    public void gridControl()
    {       
        for (int i = 0; i < dtGridSiparis.RowCount-1; i++)
            {
            order.productID =(dtGridSiparis.Rows[i].Cells[0].Value).ToString();
            order.productName = dtGridSiparis.Rows[i].Cells[1].Value.ToString();
            order.customer = dtGridSiparis.Rows[i].Cells[2].Value.ToString();
            order.faturaNo = dtGridSiparis.Rows[i].Cells[3].Value.ToString();
            order.miktar = dtGridSiparis.Rows[i].Cells[4].Value.ToString();
            order.price = dtGridSiparis.Rows[i].Cells[5].Value.ToString();
            }
            ctx.siparisDetayys.InsertOnSubmit(order);
            ctx.SubmitChanges();
    }

我嘗試使用LINQ插入我的數據庫中的所有數據,即使我執行了for循環,但仍然只添加了一行。

是的,這是因為您實際上僅插入一個order記錄,並希望您從循環最后一次迭代中收集的最后一個order記錄。 將保存移動到循環內的DB代碼行,如下所示

    for (int i = 0; i < dtGridSiparis.RowCount-1; i++)
        {
        order.productID =(dtGridSiparis.Rows[i].Cells[0].Value).ToString();
        order.productName = dtGridSiparis.Rows[i].Cells[1].Value.ToString();
        order.customer = dtGridSiparis.Rows[i].Cells[2].Value.ToString();
        order.faturaNo = dtGridSiparis.Rows[i].Cells[3].Value.ToString();
        order.miktar = dtGridSiparis.Rows[i].Cells[4].Value.ToString();
        order.price = dtGridSiparis.Rows[i].Cells[5].Value.ToString();

        //Save or Insert the record to database
        ctx.siparisDetayys.InsertOnSubmit(order);
        ctx.SubmitChanges();
        }

Rahul說了什么,還添加了第一行“ siparisDetayy order = new siparisDetayy();” 在循環內。 您必須為每一行創建一個新實體。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM