SQLXML系列之一:SQLXML初体验——用XML代替SQL来操作数据库
2008-12-06 10:16:03 来源:WEB开发网table
{}{
width:100%;
border:solid 1px orange;
}
heading1
{}{
font-size:14px;
font-weight:bold;
}
.heading2
{}{
font-size :12px;
font-weight :bold;
background-color:#F2F1AE
}
td
{}{
border:solid 1px #CFA441;
background-color:#BAE7C2;
font-family:Verdana;
font-size:10px;
}
6. 实现查询: Utility.cs
using System;
这是真个查询的实现过程,我们通过方法ExecuteIntoStream把生成的XML,通过指定的XSLT转化生成HTML,最终他们一个Stream中——因为我们会在Web Page的Code Behind中直接调用这个方法,把HTML直接送到HttpResponseStream中,从而把它显示出来。在这个方法中我们可以看到,我们首先通过一个基于SQLOLEDB的Connection String 创建一个SqlXmlCommand,SqlXmlCommand和ADO.NET中的DbCommand很相似,用于执行所有的Data Access操作,我们想使用DbCommand一样为它指定CommandType,CommandText,Parameter,我们还可以通过XslPath属性把XSLT的路径传给SqlXmlCommand,他便会自动实现转化,最后调用ExecuteToStream把最终的结果方法Stream对象中。
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using Microsoft.Data.SqlXml;
using System.Threading;
/**//// <summary>
/// Summary description for Utility
/// </summary>
public static class Utility
{
const string CONNECTION_STRING = "Provider=SQLOLEDB;Data Source=JIANGJINNAN;User id=testUser;password=password;Initial Catalog=MyTestDb";
public static string SqlTemplaeFile
{
get
{
return HttpContext.Current.Server.MapPath("Template/Order_Sql.xml");
}
}
public static string XpathTemplaeFile
{
get
{
return HttpContext.Current.Server.MapPath("Template/Order_Xpath.xml");
}
}
public static string OrderXsltFile
{
get
{
return HttpContext.Current.Server.MapPath("Transform/Order.xsl");
}
}
public static string OrderDetailXsltFile
{
get
{
return HttpContext.Current.Server.MapPath("Transform/OrderDetail.xsl");
}
}
public static void ExecuteIntoStream(Stream stream, SqlXmlCommandType commandType, string commandText,object orderID,string xsltPath)
{
try
{
SqlXmlCommand command = new SqlXmlCommand(CONNECTION_STRING);
command.CommandType = commandType;
command.CommandText = commandText;
command.XslPath = xsltPath;
command.ClearParameters();
SqlXmlParameter parameter = command.CreateParameter();
parameter.Name = "@orderID";
parameter.Value = orderID.ToString();
command.ExecuteToStream(stream);
}
catch (Exception ex)
{
throw ex;
}
}
}
更多精彩
赞助商链接