November 2003
Intermediate to advanced
476 pages
14h 38m
English
Because
both the relational and hierarchical views are XML, one can be
transformed into the other with an XSLT transformation. A program to
transform the DataSet from one format to another
is shown in Example 11-14.
using System;
using System.Data;
using System.Data.SqlClient;
using System.Xml;
using System.Xml.Xsl;
public class TransformData {
public static void Main(string [ ] args) {
DataSet dataSet = new DataSet("AngusHardware");
SqlConnection connection = new SqlConnection(
"Initial Catalog=AngusHardware; Integrated Security=SSPI; User ID=sa");
SqlDataAdapter customersAdapter = new SqlDataAdapter(
"SELECT * FROM customers", connection);
SqlDataAdapter couponsAdapter = new SqlDataAdapter(
"SELECT * FROM coupons", connection);
SqlDataAdapter couponRedemptionsAdapter = new SqlDataAdapter(
"SELECT * FROM coupon_redemptions", connection);
customersAdapter.Fill(dataSet, "customers");
couponsAdapter.Fill(dataSet, "coupons");
couponRedemptionsAdapter.Fill(dataSet, "coupon_redemptions");
XmlDataDocument doc = new XmlDataDocument(dataSet);
XmlTextWriter writer = new XmlTextWriter(Console.Out);
writer.Formatting = Formatting.Indented;
XslTransform transform = new XslTransform( );
transform.Load("Coupons.xsl");
transform.Transform(doc, null, writer);
}
}
You’ve seen most of
this already at one point or another. The main variation that you
have not seen yet is the inclusion of several
SqlDataAdapter instances ...