LINQ to SQL语句(11)之Update

LINQ to SQL语句(11)之Update

更新(Update)

说明:更新操作,先获取对象,进行修改操作之后,直接调 用SubmitChanges()方法即可提交。注意,这里是在同一个DataContext中,对于 不同的DataContex看下面的讲解。

1.简单形式

Customer cust =

  db.Customers.First(c => c.CustomerID == "ALFKI");

cust.ContactTitle = "Vice President";

db.SubmitChanges();

语句描述:使用 SubmitChanges将对检索到的一个Customer对象做出的更新保持回数据库。

2.多项更改

var q = from p in db.Products

     where p.CategoryID == 1

    select p;

foreach (var p in q)

{

  p.UnitPrice += 1.00M;

}

db.SubmitChanges ();

语句描述:使用SubmitChanges将对检索到的进行的更新保持回数 据库。

原文地址:https://www.cnblogs.com/liubo/p/2381560.html