The Class SQLXMLAdapter
The class SqlXMLAdapter provides all the necessary functions for retrieving relational data through XML views and loading it into a dataset object. For example, you can:
- Load data with an XPath expression with the Fill method of the SqlXMLAdapter class
- Work with the data and update the database by calling the Update function
SQLXML analyses the dataset object and calculates all necessary update operations. It then translates these changes into UpdateGrams, which describe changes on a database in an XML format. The database internally translates UpdateGrams into SQL statements (INSERT, UPDATE, DELETE).
The following listing shows how you can load data into a dataset object, edit the data, and update the database with the changes:
using System;
using System.Data;
using Microsoft.Data.SqlXML;
public class SqlXMLAdapterSample
{
public static void Main()
{
String strConnectionString =
Provider=SQLOLEDB;Server=(local);
database=Northwind;user id=sa;pwd=;
SqlXmlCommand cmd = new
SqlXmlCommand(strConnectionString);
cmd.CommandType = SqlXmlCommandType.XPath;
cmd.CommandText = /Customer[@CustomerID=ALFKI];
cmd.SchemaPath = Customers.xsd;
DataSet ds = new DataSet();
SqlXMLAdapter adapter = new SqlXMLAdapter(cmd);
adapter.Fill(ds);
// Now you have can edit the data in the dataset
// object
adapter.Update(ds);
}
}
In the first line, you create a new instance of the class SqlXMLCommand. This class represents a command, which is executed through SQLXML. Then you set the property CommandType to SqlXMLCommandType.XPath, which tells SQLXML that you are using an XPath statement for the data-retrieving process. In this case, you are using the XPath statement /Customer[@CustomerID=ALFKI]. As a result, you get the customer with the CustomerID ALKFI. Finally, you must tell SQLXML which XML view you are using. You can do this with the property SchemaPath.
Once youve finished that, you can create an instance of the class SqlXMLAdapter and retrieve the requested data with a call of the method Fill. By now you have a normal dataset object, which you already know from ADO.NET. With a call of the method Update, SQLXML creates all the necessary UpdateGrams and executes them against the database.