共用方式為


HOW TO:顯示變更集 (LINQ to SQL)

更新: November 2007

您可以使用 GetChangeSet 來檢視 DataContext 所追蹤的變更。

範例

下列範例會擷取所在城市是 London 的客戶、將城市變更為 Paris,並將變更送回給資料庫。

Dim db As New Northwnd("c:\northwnd.mdf")

Dim custQuery = _
    From cust In db.Customers _
    Where (cust.City = "London") _
    Select cust

For Each custObj As Customer In custQuery
    Console.WriteLine("CustomerID: {0}", custObj.CustomerID)
    Console.WriteLine(vbTab & "Original value: {0}", custObj.City)
    custObj.City = "Paris"
    Console.WriteLine(vbTab & "Updated value: {0}", custObj.City)
Next

Dim cs As ChangeSet = db.GetChangeSet()
Console.Write("Total changes: {0}", cs)
' Freeze the console window.
Console.ReadLine()

db.SubmitChanges()
Northwnd db = new Northwnd(@"c:\northwnd.mdf");

var custQuery =
    from cust in db.Customers
    where cust.City == "London"
    select cust;

foreach (Customer custObj in custQuery)
{
    Console.WriteLine("CustomerID: {0}", custObj.CustomerID);
    Console.WriteLine("\tOriginal value: {0}", custObj.City);
    custObj.City = "Paris";
    Console.WriteLine("\tUpdated value: {0}", custObj.City);
}

ChangeSet cs = db.GetChangeSet();
Console.Write("Total changes: {0}", cs);
// Freeze the console window.
Console.ReadLine();

db.SubmitChanges();

這個程式碼的輸出會與下列類似。請注意,結尾的摘要會顯示進行了八項變更。

CustomerID: AROUT

    Original value: London

    Updated value: Paris

CustomerID: BSBEV

    Original value: London

    Updated value: Paris

CustomerID: CONSH

    Original value: London

    Updated value: Paris

CustomerID: EASTC

    Original value: London

    Updated value: Paris

CustomerID: NORTS

    Original value: London

    Updated value: Paris

CustomerID: PARIS

    Original value: London

    Updated value: Paris

CustomerID: SEVES

    Original value: London

    Updated value: Paris

CustomerID: SPECD

    Original value: London

    Updated value: Paris

 

Total changes: {Added: 0, Removed: 0, Modified: 8}

請參閱

其他資源

偵錯支援 (LINQ to SQL)