Книга: C# 2008 Programmer

Updating Rows

Updating Rows

Updating rows using LINQ to SQL is straightforward — you retrieve the record you need to modify:

DataClasses1DataContext database = new DataClasses1DataContext();
title bookTitle = (from t in database.titles
 where (t.title_id == "BU5555")
 select t).Single();

The Single() method returns the only element of a sequence, and throws an exception if there is not exactly one element in the sequence.

Modify the field you want to change:

bookTitle.title1 = "How to Motivate Your Staff";

And submit the changes using the SubmitChanges() method:


The query can alternatively be written using the method syntax, like this:

title bookTitle =
 database.titles.Single(t => t.title_id == "BU5555");

Оглавление книги

Генерация: 0.853. Запросов К БД/Cache: 3 / 0
Вверх Вниз