LINQ to SQL Introduction

Example. A Simple Example Updating the ContactName of a Customer in the Northwind Database
//  Create a DataContext.
Northwind db = new Northwind(@"Data Source=.\SQLEXPRESS;Initial Catalog=Northwind");

//  Retrieve customer LAZYK.
Customer cust = (from c in db.Customers
                 where c.CustomerID == "LAZYK"
                 select c).Single<Customer>();

//  Update the contact name.
cust.ContactName = "Ned Plimpton";

try
{
  //  Save the changes.
  db.SubmitChanges();
}
//  Detect concurrency conflicts.
catch (ChangeConflictException)
{
  //  Resolve conflicts.
  db.ChangeConflicts.ResolveAll(RefreshMode.KeepChanges);
}

NOTE

This example requires generation of entity classes, which I will cover later in this chapter.

In Listing 12-1, I used LINQ to SQL to ...

Get Pro LINQ: Language Integrated Query in C# 2008 now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.