如何在Windows Phone App中更新Linq-To-Sql中的记录

时间:2014-07-26 07:29:07

标签: windows-phone-8 local-storage

我使用下面的代码在购物车数据库中插入新商品:

using (MFTDataContext Cartdb = new MFTDataContext(strConnectionString))
                        {
                            Database InsertItemCart = new Database
                            {
                                ProductID = int.Parse(product_id[0]),
                                ProductName = product_name[0],
                                ProductPrice = int.Parse(product_price[0]),
                                ProductQuantity = singletonInstance.Quantity,
                                ProductTotalPrice = product_totalpriceINT,
                                ProductImageURL = product_image[0]
                            };


                            Cartdb.MFTCart.InsertOnSubmit(InsertItemCart);


                          Cartdb.SubmitChanges();

现在使用什么代码来更新同一记录。假设singletonInstance.Quantity从1更改为5.现在如何更新相同的记录。我不想创造新纪录。如何更新相同的记录

1 个答案:

答案 0 :(得分:0)

private void Update(int itemID, int newQuantityValue)
{
    using (var context = new YouContextType())
    {
        var item = (from item in context.YourEntity
                    where item.ID == itemID
                    select item).Single();

        item.Quantity = newQuantityValue;
        context.SubmitChanges();
    }
}