Mapping SQL Server Data to Classes

In Chapter 8, we saw the ExecuteXmlReader method exposed by the SqlCommand class in the SQL Server–managed provider. The ExecuteXmlReader method executes a command against the database and returns an XML reader if the output of the command can be expressed as a well-formed XML document or fragment. Let’s see what’s needed to transform that output into an instance of a class. The following code is at the heart of the example. You call into a method, the method executes an SQL XML command, the data flows into the serializer, and an instance of a particular class is returned.

Employee emp = LoadEmployeeData(empID);

The following code shows the body of the LoadEmployeeData method:

 private Employee LoadEmployeeData(int empID) ...

Get Applied XML Programming for Microsoft® .NET now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.