When the code behind button1 executes, it calls the InsertNames routine several times, passing a contact
first name, a title, and an email address to the routine. The InsertNames routine then uses those values
to insert the contacts.
When the code finishes executing, the text box on the form displays ??????Contact added successfully.??™??™ At
that point, you can query the Person.Contact table in the AdventureWorks database. The results should
look like those shown in Figure 13-1.
Figure 13-1
You can see how easy it is to perform Insert operations using LINQ to SQL and entity objects. Yet, you
are just skimming the surface. What about modifying existing records and sending the changes back?
Let??™s do an update example next. Behind button2 of your form, add the following code:
private void button2_Click(object sender, EventArgs e)
{
AdventureWorks db = new AdventureWorks("Integrated Security=sspi");
261
Part III: LINQ to SQL
try
{
var conQuery =
from con in db.Contacts
where con.FirstName == "Scott"
select con;
// there are 15 Scott??™s in the table, so 15 changes should be made
foreach (Contact cont in conQuery)
{
cont.
Pages:
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421