Load(@"C:\Wrox\Linq\Chapter5\Contacts.xml");
foreach (XElement xelem in xel.Elements("Contact"))
{
Contact
Contact con = db.contact.First(co => co.ContactID ==
(int)xelem.Element("ContactID"));
con.Title = (string)xelem.Element("Title");
con.MiddleName = (string)xelem.Element("MiddleName");
con.EmailAddress = (string)xelem.Element("EmailAddress");
db.SubmitChanges();
}
label1.Text = "Update successful";
lable1.Visible = true;
Run the project and click the XML to SQL button. When the update is successful, the label on the form will
display the text ??????Update successful.??™??™ To verify the results, open a new query window in SSMS. Select the
AdventureWorks database, and execute the same query you used in the last example. Figure 7-3 shows
the results.
Figure 7-3
You have successfully read data from an XML file and updated a record in a SQL table. In the preceding
highlighted code, a query for each contact element is executed against the database, returning the
151
Part II: LINQ to XML
corresponding ContactID. In this example, a single record is returned because the query is only looking
for a specific ContactID.
Pages:
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265