更新(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將對檢索到的進行的更新保持回資料庫。

arrow
arrow
    文章標籤
    LINQ LINQ to SQL
    全站熱搜

    Jimmy 發表在 痞客邦 留言(1) 人氣()