如何使用实体框架更新数据库中的数据

时间:2019-06-09 11:10:55

标签: c# entity-framework

我想使用datagridview中的值更新数据库中的数据,但没有成功。我的目标是搜索我的数据网格视图,如果我的产品名称存在于gridview中,那么我将更新数量。

if (bunifuDataGridView1.Rows.Count > 0)
{
    foreach (DataGridViewRow row in bunifuDataGridView1.Rows)
    {
       if (Convert.ToString(row.Cells[2].Value) == bunifuTextBox11.Text)
        {
            row.Cells[5].Value = Convert.ToString(Convert.ToInt32(bunifuTextBox10.Text) + Convert.ToInt32(row.Cells[5].Value));
            found = true;

            obj5.ProductName = Convert.ToString(row.Cells[2].Value);
            obj5.CostPricePerProduct = Convert.ToInt32(row.Cells[3].Value);
            obj5.SellingPricePerProduct = Convert.ToInt32(row.Cells[4].Value);
            obj5.Quantity = Convert.ToInt32(row.Cells[5].Value);
            obj5.ExpiryDate = Convert.ToString(row.Cells[6].Value);
            obj5.ProductNumber = Convert.ToInt32(obj2.ProductNumber);
            obj5.Quantity = Convert.ToInt32(row.Cells[5].Value);

            context.Entry.state = Entrystate.modified;

            context.SaveChanges();

            inboundgoods();
            refreshcustomergrid();
        }
    }

    if (!found)
    {
        inboundgoods();
    }
}
else
{
    inboundgoods();
}

我希望我的代码能够通过datagridview搜索产品名称,如果有匹配项,它应该通过增加库存数量来更新该记录并保存在库存数据库中。

1 个答案:

答案 0 :(得分:0)

如果没有完整的应用程序,这很难调试,但是我们可以建议一些有助于调试的代码更改:

if (bunifuDataGridView1.Rows.Count > 0)
{
    foreach (DataGridViewRow row in bunifuDataGridView1.Rows)
    {
        // Compare the Product on each row, add a watch to this value to assist debugging
        var product = Convert.ToString(row.Cells[2].Value);
        if (product == bunifuTextBox11.Text) // consider rename bunfuTextBox11 to something meaningful, like 'ProductNameTextBox'
        {
            row.Cells[5].Value = Convert.ToString(Convert.ToInt32(bunifuTextBox10.Text) + Convert.ToInt32(row.Cells[5].Value)); // consider rename bunifuTextBox10 to something more meaningful like 'ProductQuantityTextBox'
            found = true;
            obj5.ProductName = Convert.ToString(row.Cells[2].Value);
            obj5.CostPricePerProduct = Convert.ToInt32(row.Cells[3].Value);
            obj5.SellingPricePerProduct = Convert.ToInt32(row.Cells[4].Value);
            obj5.Quantity= Convert.ToInt32(row.Cells[5].Value);
            obj5.ExpiraryDate = Convert.ToString(row.Cells[6].Value);
            obj5.ProductNumber = Convert.ToInt32(obj2.ProductNumber);
            obj5.Quantity = Convert.ToInt32(row.Cells[5].Value);
            //context.Entry.state=Entrystate.modified;
            // If your context has automatic change tracking enabled, this following line is not necessary
            // But you need to make sure you are setting the State on the correct object tracker instance by passing it in to the Entry method.
            var dbEntry = g.Entry(obj5);            
            if (dbEntry.State == EntryState.Detached)
                dbEntry.State = EntryState.Added;
            else
                dbEntry.State = EntryState.Modified;
            context.SaveChanges();
            inboundgoods();
            refreshcustomergrid();
        }
    }

    if (!found)
    {
        inboundgoods();
    }
}

else
{
    inboundgoods();
}

如果在调试过程中没有到达代码的found = true;行,然后查看比较逻辑,查找拼写和空格问题,那么如果您输入或存储数据,则可能需要将比较更改为类似的内容可能有空格或字母大小写不一致。

if (product.Trim().Equals(bunifuTextBox11.Text.Trim(), StringComparison.OrdinalIgnoreCase))
  

花时间在数据输入字段控件中使用有意义的名称,这将使您的代码更易于阅读和理解,尤其是当您将代码示例发布到诸如SO之类的论坛时!