O'Reilly logo

Pro LINQ: Language Integrated Query in C# 2008 by Joseph C. Rattz Jr.

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required