Felix  RuthenbergHow to get the ID of a newly inserted data tuple


Imagine a scenarion where you want to insert a new data row into a database and the database automatically generates an entry for the ID of that data tuple (which is the case in most business scenarios).

If you use Linq to Sql you can easily access the ID after you have committed the SubmitChanges() method on the data context.  And how?  Just ask the object for its appropriate property:

NorthwindDataContext ndc = new NorthwindDataContext();
Order newOrder = new Order { CustomerID = "ALFKI", EmployeeID = 4 };
ndc.Orders.InsertOnSubmit(newOrder);
ndc.SubmitChanges();
Console.WriteLine("The ID of the newly inserted order is: {0}",newOrder.OrderID);

One of my attendees in Darmstadt already mentioned it could be as simple as this and I am happy to announce him right!



Kategorien: C#;LINQ 21.01.2009 10:35:50


 


Neuen Kommentar einfügen:

  Titel:   
  Name:
  E-Mail:
  Kommentar:

 
 


Kommentare




© Copyright 2008 ppedv AG