if (publisher.Count() < 0)
Console.WriteLine("({0})", publisher.First().pub_name);
}
The output looks something like this:
Cooking with Computers: Surreptitious Balance Sheets (Algodata Infosystems)
You Can Combat Computer Stress! (New Moon Books)
How to Motivate Your Employees Straight Talk About Computers (Algodata Infosystems)
Silicon Valley Gastronomic Treats (Binnet & Hardley)
The Gourmet Microwave (Binnet & Hardley)
The Psychology of Computer Cooking (Binnet & Hardley)
But Is It User Friendly? (Algodata Infosystems)
Secrets of Silicon Valley (Algodata Infosystems)
Net Etiquette (Algodata Infosystems)
Computer Phobic AND Non-Phobic Individuals: Behavior Variations (Binnet & Hardley)
Is Anger the Enemy? (New Moon Books)
Life Without Fear (New Moon Books)
Prolonged Data Deprivation: Four Case Studies (New Moon Books)
Emotional Security: A New Algorithm (New Moon Books)
Onions, Leeks, and Garlic: Cooking Secrets of the Mediterranean (Binnet & Hardley)
Fifty Years in Buckingham Palace Kitchens (Binnet & Hardley)
Sushi, Anyone? (Binnet & Hardley)
To insert a row into a table, use the InsertOnSubmit()
method. For example, the following code inserts a new author into the authors table:
DataClasses1DataContext database = new DataClasses1DataContext();
author a = new author() {
au_id = "789-12-3456",
au_fname = "James",
au_lname = "Bond",
phone = "987654321"
};
//---record is saved to object model---
database.authors.InsertOnSubmit(a);
Note that the InsertOnSubmit()
method only affects the object model; it does not save the changes back to the database. To save the changes back to the database, you need to use the SubmitChanges()
method:
//---send changes to database---
database.SubmitChanges();
What happens when you need to insert a new book title from a new author? As you saw earlier, the titles
table is related to the titleauthors
via the title_id
field, while the authors
table is related to the titleauthors
table via the author_id
field. Therefore, if you insert a new row into the titles table, you need to insert a new row into the authors
and titleauthors
tables as well.
To do so, you first create a new author
and title
row:
DataClasses1DataContext database = new DataClasses1DataContext();
author a = new author() {
au_id = "123-45-6789",
au_fname = "Wei-Meng",
au_lname = "Lee",
phone = "123456789"
};
title t = new title() {
title_id = "BU5555",
title1 = "How to Motivate Your Employees",
pubdate = System.DateTime.Now,
type = "business"
};
Then, add a new titleauthor
row by associating its author
and title
properties with the new title
and author
row you just created:
titleauthor ta = new titleauthor() {
author = a,
title = t
};
Finally, save the changes to the object model and submit the changes to the database:
//---record is saved to object model---
database.titleauthors.InsertOnSubmit(ta);
//---send changes to database---
database.SubmitChanges();
Notice that you do not need to worry about indicating the title_id
and author_id
fields in the titleauthors
table; LINQ to SQL does those for you automatically.
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:
database.SubmitChanges();
The query can alternatively be written using the method syntax, like this:
title bookTitle =
database.titles.Single(t => t.title_id == "BU5555");
To delete a row, you first retrieve the row to delete:
DataClasses1DataContext database =
new DataClasses1DataContext(); //---find author ---
var author = from a in database.authors
where a.au_id == "789-12-3456"
select a;
Then, locate the row to delete by using the First()
method, and finally call the DeleteOnSubmit()
method to delete the row:
if (author.Count() > 0) {
database.authors.DeleteOnSubmit(author.First());
database.SubmitChanges();
}
The First()
method returns the first element of a sequence.
If you have multiple rows to delete, you need to delete each row individually, like this:
//---find author---
var authors = from a in database.authors
where a.au_id == "111-11-1111" ||
a.au_id == "222-22-1111"
select a;
foreach (author a in authors) {
database.authors.DeleteOnSubmit(a);
}
database.SubmitChanges();
So far the deletion works only if the author to be deleted has no related rows in the titleauthors
and titles
tables. If the author has associated rows in the titleauthors
and titles
tables, these examples cause an exception to be thrown because the deletions violate the referential integrity of the database (see Figure 14-21).
Читать дальше