Use Templates
When you use SQLXML, you can place your XPath, XQuery, or SQL statements in templates. Templates also are XML documents that encapsulate database queries. You could say that templates separate your database queries from your program code. When you include hard-coded queries in your programs, templates make the process of maintaining those programs easier--you dont have to change the program code when you change a query.
The following listing shows how to use templates:
using System;
using System.IO;
using System.Data;
using Microsoft.Data.SqlXML;
public class TemplateSample
{
public static void Main()
{
String strConnectionString =
Provider=SQLOLEDB;Server=(local);
database=Northwind;user id=sa;pwd=;
Stream stream;
SqlXMLCommand cmd = new
SqlXMLCommand(strConnectionString);
cmd.CommandStream = new FileStream(Template.XML,
FileMode.Open, FileAccess.Read);
cmd.CommandType = SqlXMLCommandType.Template;
stream = cmd.ExecuteStream();
StreamReader reader = new StreamReader(stream);
Console.Writeline(reader.ReadToEnd());
Console.ReadLine();
}
}
The contents of the template file Template.XML is shown in the following lines:
<ROOT XMLns:sql=urn:schemas-microsoft-com:XML-sql>
<sql:query>
SELECT * FROM Customers
WHERE CustomerID=ALFKI
FOR XML AUTO
</sql:query>
</ROOT>