Nota
L'accesso a questa pagina richiede l'autorizzazione. È possibile provare ad accedere o modificare le directory.
L'accesso a questa pagina richiede l'autorizzazione. È possibile provare a modificare le directory.
È possibile visualizzare le modifiche rilevate da un DataContext usando GetChangeSet.
Esempio
L'esempio seguente recupera i clienti la cui città è Londra, modifica la città in Parigi e invia le modifiche al database.
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: {custObj.CustomerID}");
Console.WriteLine($"\tOriginal value: {custObj.City}");
custObj.City = "Paris";
Console.WriteLine($"\tUpdated value: {custObj.City}");
}
ChangeSet cs = db.GetChangeSet();
Console.Write("Total changes: {0}", cs);
// Freeze the console window.
Console.ReadLine();
db.SubmitChanges();
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()
L'output di questo codice è simile al seguente. Si noti che il riepilogo alla fine mostra che sono state apportate otto modifiche.
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}