MiddleName = "L";
cont.NameStyle = 1;
}
db.SubmitChanges();
textBox1.Text = "Contacts modified successfully";
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
Before you run this code, spend a few minutes looking at what it??™s doing. First, you have a standard
LINQ query that is populating the entity class with all contacts whose first name is Scott. Each contact is
then iterated over, changing the middle initial to ??????L??™??™.
Just like the previous example, the SubmitChanges() method is used to usher the changes to the object
back to the database.
What about deleting? The great thing about LINQ and LINQ to SQL is that all these operations are
extremely similar. To illustrate, add the following code behind button3 of your form:
private void button3_Click(object sender, EventArgs e)
{
AdventureWorks db = new AdventureWorks("Integrated Security=sspi");
try
{
var conQuery =
from con in db.Contacts
where con.LastName == "Klein"
select con;
foreach (Contact cont in conQuery)
{
db.Contacts.Remove(cont);
}
db.
Pages:
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422