April 24, 2007, 6:32 a.m.
posted by fractal
Using XML with SQL ServerThe .NET team is not the only group at Microsoft that's been working with XML. Over the past several releases, Microsoft SQL Server has become increasingly integrated with XML. In the current release, you can generate XML with SQL statements, using Microsoft T-SQL extensions to the SQL standard query language. You can also update SQL Server tables by sending properly formed XML messages, called DiffGrams, to a SQL Server database. In this section, you'll learn the basics of interacting with SQL Server via XML. Generating XML with SQL Statements
Understanding the FOR XML ClauseSQL Server enables you to retrieve the results of any query as XML rather than as a SQL resultset. To do this, you use the Microsoft-specific FOR XML clause. You can use a variety of options in the FOR XML clause to customize the XML that SQL Server generates. The first option is FOR XML RAW. When you use raw mode with FOR XML, SQL Server returns one element (always named row) for each row of the resultset, with the individual columns represented as attributes. For example, consider this query:
SELECT Customers.CustomerID, Customers.CompanyName,
Orders.OrderID, Orders.OrderDate
FROM Customers INNER JOIN Orders
ON Customers.CustomerID = Orders.CustomerID
WHERE Country = 'Brazil' AND
OrderDate BETWEEN '1997-03-15' AND '1997-04-15'
FOR XML RAW
If you execute this query (for example, using SQL Query Analyzer) in the Northwind sample database, you'll get back these results: <row CustomerID="RICAR" CompanyName="Ricardo Adocicados" OrderID="10481" OrderDate="1997-03-20T00:00:00"/> <row CustomerID="QUEEN" CompanyName="Queen Cozinha" OrderID="10487" OrderDate="1997-03-26T00:00:00"/> <row CustomerID="COMMI" CompanyName="Comércio Mineiro" OrderID="10494" OrderDate="1997-04-02T00:00:00"/> <row CustomerID="TRADH" CompanyName="Tradiça[dd]o Hipermercados" OrderID="10496" OrderDate="1997-04-04T00:00:00"/> NOTE Result Formatting SQL Query Analyzer returns XML results as one long string. I've reformatted these results for easier display on the printed page. If you have trouble seeing all the results in SQL Query Analyzer, select Tools, Options, Results, and increase the Maximum Character Width setting. If the query output contains binary columns, you must include the BINARY BASE64 option after the FOR XML clause to avoid a runtime error: SELECT EmployeeID, Photo FROM Employees FOR XML RAW, BINARY BASE64 With this option, standard Base64 coding is used to encode any binary columns in the output XML. The second variant of the FOR XML clause is FOR XML AUTO. When you use auto mode with FOR XML, nested tables in the resultset are represented as nested elements in the XML. Columns are still represented as attributes. For example, here's a query that uses FOR XML AUTO:
SELECT Customers.CustomerID, Customers.CompanyName,
Orders.OrderID, Orders.OrderDate
FROM Customers INNER JOIN Orders
ON Customers.CustomerID = Orders.CustomerID
WHERE Country = 'Brazil' AND
OrderDate BETWEEN '1997-03-15' AND '1997-04-15'
FOR XML AUTO
Here's the corresponding resultset:
<Customers CustomerID="RICAR"
CompanyName="Ricardo Adocicados">
<Orders OrderID="10481"
OrderDate="1997-03-20T00:00:00"/>
</Customers>
<Customers CustomerID="QUEEN"
CompanyName="Queen Cozinha">
<Orders OrderID="10487"
OrderDate="1997-03-26T00:00:00"/>
</Customers>
<Customers CustomerID="COMMI"
CompanyName="Comércio Mineiro">
<Orders OrderID="10494"
OrderDate="1997-04-02T00:00:00"/>
</Customers>
<Customers CustomerID="TRADH"
CompanyName="Tradição Hipermercados">
<Orders OrderID="10496"
OrderDate="1997-04-04T00:00:00"/>
</Customers>
Note that in this resultset, the Orders element is nested within the Customers element for each order. If there were multiple orders for a single customer, the Orders element would repeat as many times as necessary. There's a second variant of FOR XML AUTO. You can include the ELEMENTS option to represent columns as elements rather than as attributes. Here's query that uses this option:
SELECT Customers.CustomerID, Customers.CompanyName,
Orders.OrderID, Orders.OrderDate
FROM Customers INNER JOIN Orders
ON Customers.CustomerID = Orders.CustomerID
WHERE Country = 'Brazil' AND
OrderDate BETWEEN '1997-03-15' AND '1997-04-15'
FOR XML AUTO, ELEMENTS
Here's the corresponding resultset:
<Customers>
<CustomerID>RICAR</CustomerID>
<CompanyName>Ricardo Adocicados</CompanyName>
<Orders>
<OrderID>10481</OrderID>
<OrderDate>1997-03-20T00:00:00</OrderDate>
</Orders>
</Customers>
<Customers>
<CustomerID>QUEEN</CustomerID>
<CompanyName>Queen Cozinha</CompanyName>
<Orders>
<OrderID>10487</OrderID>
<OrderDate>1997-03-26T00:00:00</OrderDate>
</Orders>
</Customers>
<Customers>
<CustomerID>COMMI</CustomerID>
<CompanyName>Comércio Mineiro</CompanyName>
<Orders>
<OrderID>10494</OrderID>
<OrderDate>1997-04-02T00:00:00</OrderDate>
</Orders>
</Customers>
<Customers>
<CustomerID>TRADH</CustomerID>
<CompanyName>Tradição Hipermercados</CompanyName>
<Orders>
<OrderID>10496</OrderID>
<OrderDate>1997-04-04T00:00:00</OrderDate>
</Orders>
</Customers>
The final variant of FOR XML is FOR XML EXPLICIT. In explicit mode, you must construct your query so as to create a resultset with the first column named Tag and the second column named Parent. These columns create a self-join in the resultset that is used to determine the hierarchy of the created XML file. Here's a relatively simple query in explicit mode:
SELECT 1 AS Tag, NULL AS Parent,
Customers.CustomerID AS [Customer!1!CustomerID],
Customers.CompanyName AS [Customer!1!CompanyName],
NULL AS [Order!2!OrderID],
NULL AS [Order!2!OrderDate]
FROM Customers WHERE COUNTRY = 'Brazil'
UNION ALL
SELECT 2, 1,
Customers.CustomerID, Customers.CompanyName,
Orders.OrderID, Orders.OrderDate
FROM Customers INNER JOIN Orders
ON Customers.CustomerID = Orders.CustomerID
WHERE Country = 'Brazil' AND
OrderDate BETWEEN '1997-03-15' AND '1997-04-15'
ORDER BY [Customer!1!CustomerID], [Order!2!OrderID]
FOR XML EXPLICIT
The resulting XML from this query is as follows:
<Customer CustomerID="COMMI"
CompanyName="Comércio Mineiro">
<Order OrderID="10494"
OrderDate="1997-04-02T00:00:00"/>
</Customer>
<Customer CustomerID="FAMIA"
CompanyName="Familia Arquibaldo"/>
<Customer CustomerID="GOURL"
CompanyName="Gourmet Lanchonetes"/>
<Customer CustomerID="HANAR"
CompanyName="Hanari Carnes"/>
<Customer CustomerID="QUEDE"
CompanyName="Que Delícia"/>
<Customer CustomerID="QUEEN"
CompanyName="Queen Cozinha">
<Order OrderID="10487"
OrderDate="1997-03-26T00:00:00"/>
</Customer>
<Customer CustomerID="RICAR"
CompanyName="Ricardo Adocicados">
<Order OrderID="10481"
OrderDate="1997-03-20T00:00:00"/>
</Customer>
<Customer CustomerID="TRADH"
CompanyName="Tradição Hipermercados">
<Order OrderID="10496"
OrderDate="1997-04-04T00:00:00"/>
</Customer><Customer CustomerID="WELLI"
CompanyName="Wellington Importadora"/>
Note that in this case even customers without orders in the specified time period are included, because the first half of the query retrieves all customers from Brazil. Explicit mode allows you the finest control over the generated XML, but it's also the most complex mode to use in practice. You should stick to raw or auto mode whenever possible. Finally, you can generate schema information as part of a SQL Server query by including the XMLDATA option in the query. You can do this in any of the FOR XML modes. For example, here's a query you saw earlier in this section with the XMLDATA option added:
SELECT Customers.CustomerID, Customers.CompanyName,
Orders.OrderID, Orders.OrderDate
FROM Customers INNER JOIN Orders
ON Customers.CustomerID = Orders.CustomerID
WHERE Country = 'Brazil' AND
OrderDate BETWEEN '1997-03-15' AND '1997-04-15'
FOR XML AUTO, ELEMENTS, XMLDATA
The resulting XML is as follows:
<Schema name="Schema1"
xmlns="urn:schemas-microsoft-com:xml-data"
xmlns:dt="urn:schemas-microsoft-com:datatypes">
<ElementType name="Customers" content="eltOnly"
model="closed" order="many">
<element type="Orders" maxOccurs="*"/>
<element type="CustomerID"/>
<element type="CompanyName"/>
</ElementType>
<ElementType name="CustomerID" content="textOnly"
model="closed" dt:type="string"/>
<ElementType name="CompanyName" content="textOnly"
model="closed" dt:type="string"/>
<ElementType name="Orders" content="eltOnly"
model="closed" order="many">
<element type="OrderID"/>
<element type="OrderDate"/>
</ElementType>
<ElementType name="OrderID" content="textOnly"
model="closed" dt:type="i4"/>
<ElementType name="OrderDate" content="textOnly"
model="closed" dt:type="dateTime"/>
</Schema>
<Customers xmlns="x-schema:#Schema1">
<CustomerID>RICAR</CustomerID>
<CompanyName>Ricardo Adocicados</CompanyName>
<Orders>
<OrderID>10481</OrderID>
<OrderDate>1997-03-20T00:00:00</OrderDate>
</Orders>
</Customers>
<Customers xmlns="x-schema:#Schema1">
<CustomerID>QUEEN</CustomerID>
<CompanyName>Queen Cozinha</CompanyName>
<Orders>
<OrderID>10487</OrderID>
<OrderDate>1997-03-26T00:00:00</OrderDate>
</Orders>
</Customers>
<Customers xmlns="x-schema:#Schema1">
<CustomerID>COMMI</CustomerID>
<CompanyName>Comércio Mineiro</CompanyName>
<Orders>
<OrderID>10494</OrderID>
<OrderDate>1997-04-02T00:00:00</OrderDate>
</Orders>
</Customers>
<Customers xmlns="x-schema:#Schema1">
<CustomerID>TRADH</CustomerID>
<CompanyName>Tradição Hipermercados</CompanyName>
<Orders>
<OrderID>10496</OrderID>
<OrderDate>1997-04-04T00:00:00</OrderDate>
</Orders>
</Customers>
Using ExecuteXmlReader() MethodADO.NET provides a means to integrate SQL Server's XML capabilities with the .NET Framework classes. The ExecuteXmlReader() method of the SqlCommand object enables you to retrieve an XmlReader directly from a SQL statement, provided that the SQL statement uses the FOR XML clause. Step-by-Step 2.15 shows you how.
WARNING Not a Valid Document It's tempting to think that you can read an XmlDocument object directly from the XmlReader object returned by the ExecuteXmlReader() method. Unfortunately, if you try this you'll find that it generates an error. This is because the XML returned by FOR XML queries is well-formed, but it lacks an XML declaration and a root node, and is therefore an XML fragment and not a valid XML document. Updating SQL Server Data by Using XML
You can also update SQL Server data by using special XML messages called DiffGrams. The .NET Framework uses DiffGrams internally as a means of serializing changes in a DataSet object. For example, if you pass the changes in a DataSet object from one tier to another, the .NET Framework uses a DiffGram to send the changes. You can also use DiffGrams yourself to update data in SQL Server. However, before you can do so, you need to install some additional software. This software is the SQLXML Managed Classes, an interface between SQL Server and the .NET Framework. In this section, you learn how to install this software and then how to use DiffGrams to modify SQL Server data. Installing SQLXMLAlthough SQL Server 2000 includes some XML support (for example, the FOR XML syntax is built into the product) there have been many advances in XML since that version of SQL Server was released. Microsoft has kept SQL Server in tune with these advances by issuing a series of free upgrade packages with the general name of SQLXML. As of this writing, the current release of SQLXML is SQLXML 3.0 SP1. This package includes the following features:
To install SQLXML, you need to download the current release directly from Microsoft's web site. You can always find the current release by starting at the SQLXML home page, msdn.microsoft.com/nhp/default.asp?contentid=28001300. Before you run the installation, be sure you have the following prerequisite software installed:
SQLXML 3.0 also depends on release 4.0 of the MSXML parser. If this component is not present on your computer, it will be installed as part of the SQLXML installation. To install SQLXML, download and run the executable. You can either choose to install all components, or select specific components to install. Using DiffGramsAfter you've installed SQLXML, you can use the SqlXmlCommand object to execute a DiffGram, as shown in Step-by-Step 2.16.
You can think of a DiffGram as a before-and-after snapshot of a part of a SQL Server table. In this case, the first part of the XML file lists a row in the Customers table and indicates that it has been modified. The second part of the DiffGram contains the original data from the SQL Server table. SQL Server can use this data to find the row to be modified. In addition to the DiffGram, this code requires a schema file that maps the element names in the DiffGram back to tables and columns in the SQL Server database. The sql:relation attribute in the schema file indicates the table mapping, whereas the sql:field attributes indicate the field mappings. DiffGrams can insert or delete data as well as modify data. For an insertion, the DiffGram contains the data for the new row and no old data. For a deletion, the DiffGram contains the row to be deleted but no new row. For more information on the DiffGram format, refer to the help files that are installed as a part of the SQLXML package.
|
- Comment


