繁体   English   中英

如何使用Entity Framework更新数据库中的数据

[英]How to update data in database using 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中搜索产品名称,如果匹配,它应该通过增加库存数量并保存在库存数据库中来更新该记录。

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

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等论坛时!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM