内容导航 |
* 引言 * 配置SQL Server 2000的IIS虚拟目录 * 使用 HTTP 执行 SQL 语句 * 使用XML模板进行查询 * XPath查询 架构和模板 |
Set ObjXML = CreateObject("SQLVDir.SQLVDirControl") ObjXML.Connect 'Connect to the local computer and Web site "1" Set ObjVDirs = ObjXML.SQLVDirs Set ObjVDir = ObjVDirs.AddVirtualDirectory("Northwind") ObjVDir.PhysicalPath = "C:/Inetpub/wwwroot/northwind" ObjVDir.UserName = "wayne" 'SQL Server login ObjVDir.Password = "" 'SQL Server Password ObjVDir.DatabaseName = "Northwind" objVDir.AllowFlags = 73 Set objVNames = objVDir.VirtualNames objVNames.AddVirtualName "dbobject", 1, "" objVNames.AddVirtualName "schema", 2,"C:/Inetpub/wwwroot/northwind/schema" objVNames.AddVirtualName "template", 4 , "C:/Inetpub/wwwroot/northwind/template" objXML.Disconnect msgbox "Done." |
<?xml version="1.0" encoding="utf-8" ?> <root> <Customers CustomerID="ANTON" CompanyName="Antonio Moreno Taquería" ContactName="Antonio Moreno" ContactTitle="Owner" Address="Mataderos 2312" City="México D.F." PostalCode="05023" Country="Mexico" Phone="(5) 555-3932" /> </root> |
http://localhost/northwind?sql=SELECT Customer.CustomerID%2cCustomer.Contact Name%2c%5bOrder%5d.OrderID+FROM+Customers+ Customer+INNER+JOIN+Orders+%5bOrder% 5d+ON+Customer.CustomerID%3d%5bOrder% 5d.CustomerID+FOR+XML+AUTO&root=Northwind |
SELECT Customers.CustomerID, Orders.OrderID, Orders.OrderDate FROM Customers, Orders WHERE Customers.CustomerID = Orders.CustomerID ORDER BY Customers.CustomerID FOR XML RAW |
<row CustomerID="ALFKI" OrderID="10643" OrderDate="1997-08-25T00:00:00"/> <row CustomerID="ANATR" OrderID="10308" OrderDate="1996-09-18T00:00:00"/> <row CustomerID="ANATR" OrderID="10625" OrderDate="1997-08-08T00:00:00"/> <row CustomerID="AROUT" OrderID="10355" OrderDate="1996-11-15T00:00:00"/> |
SELECT C.CustomerID, O.OrderID, O.OrderDate FROM Customers C LEFT OUTER JOIN Orders O ON C.CustomerID = O.CustomerID ORDER BY C.CustomerID FOR XML RAW |
|
CREATE PROCEDURE GetXml ( @CustomerID varchar(5) ) AS BEGIN SELECT CustomerID, CompanyName,ContactName FROM Customers WHERE CustomerID LIKE @CustomerID + '%' FOR XML AUTO END |
file2.xml <Northwind xmlns:sql= "urn:schemas-microsoft-com:xml-sql"> <sql:query> SELECT Customers.CustomerID, Customers.ContactName, Orders.OrderID, Orders.CustomerID FROM Customers INNER JOIN Orders ON Customers.CustomerID = Orders.CustomerID FOR XML AUTO </sql:query> </Northwind> |
<Northwind xmlns:sql="urn:schemas-microsoft-com:xml-sql"> <sql:header> <sql:param name='CustomerID'>A</sql:param> </sql:header> <sql:query> SELECT Customers.CustomerID, Customers.ContactName, Orders.OrderID, Orders.CustomerID FROM Customers INNER JOIN Orders ON Customers.CustomerID = Orders.CustomerID WHERE Customers.CustomerID LIKE @CustomerID + '%' FOR XML AUTO </sql:query> </Northwind> |
<Northwind xmlns:sql= "urn:schemas-microsoft-com: xml-sql"> <sql:xpath-query mapping-schema= "file4.xdr"> /Customer[@CustomerID= 'ALFKI']/Order </sql:xpath-query> </Northwind> |
<?xml version="1.0" ?> <Schema xmlns="urn:schemas-microsoft-com:xml-data" xmlns:dt="urn:schemas-microsoft-com:datatypes" xmlns:sql="urn:schemas-microsoft-com:xml-sql"> <ElementType name="Customer" sql:relation="Customers"> <AttributeType name="CustomerID" dt:type="id" /> <AttributeType name="CompanyName" /> <AttributeType name="ContactName" /> <AttributeType name="City" /> <AttributeType name="Fax" /> <AttributeType name="Orders" dt:type= "idrefs" sql:id-prefix="Ord-" /> <attribute type="CustomerID" /> <attribute type="CompanyName" /> <attribute type="ContactName" /> <attribute type="City" /> <attribute type="Fax" /> <attribute type="Orders" sql:relation= "Orders" sql:field="OrderID"> <sql:relationship key-relation="Customers" key="CustomerID" foreign-relation="Orders" foreign-key="CustomerID" /> </attribute> <element type="Order"> <sql:relationship key-relation="Customers" key="CustomerID" foreign-relation="Orders" foreign-key="CustomerID" /> </element> </ElementType> <ElementType name="Order" sql:relation="Orders"> <AttributeType name="OrderID" dt:type= "id" sql:id-prefix="Ord-" /> <AttributeType name="EmployeeID" /> <AttributeType name="OrderDate" /> <AttributeType name="RequiredDate" /> <AttributeType name="ShippedDate" /> <attribute type="OrderID" /> <attribute type="EmployeeID" /> <attribute type="OrderDate" /> <attribute type="RequiredDate" /> <attribute type="ShippedDate" /> <element type="OrderDetail"> <sql:relationship key-relation="Orders" key="OrderID" foreign-relation="[Order Details]" foreign-key="OrderID" /> </element> <element type="Employee"> <sql:relationship key-relation="Orders" key="EmployeeID" foreign-relation="Employees" foreign-key="EmployeeID" /> </element> </ElementType> <ElementType name="OrderDetail" sql:relation= "[Order Details]" sql:key-fields="OrderID ProductID"> <AttributeType name="ProductID" dt:type="idref" sql:id-prefix="Prod-" /> <AttributeType name="UnitPrice"/> <AttributeType name="Quantity" /> <attribute type="ProductID" /> <attribute type="UnitPrice"/> <attribute type="Quantity" /> <element type="Discount" sql:field="Discount"/> </ElementType> <ElementType name="Discount" dt:type="string" sql:relation="[Order Details]"/> <ElementType name="Employee" sql:relation="Employees"> <AttributeType name="EmployeeID" dt:type="idref" sql:id-prefix="Emp-" /> <AttributeType name="LastName" /> <AttributeType name="FirstName" /> <AttributeType name="Title" /> <attribute type="EmployeeID"/> <attribute type="LastName" /> <attribute type="FirstName" /> <attribute type="Title" /> </ElementType> </Schema> |
<Northwind xmlns:sql= "urn:schemas-microsoft-com:xml-sql"> <sql:xpath-query mapping-schema= "listing4.xdr"> /Customer[@CustomerID= 'ALFKI']/Order/ Employee[@LastName='Suyama'] </sql:xpath-query> </Northwind> |
<Northwind xmlns:sql= "urn:schemas-microsoft-com:xml-sql"> <Employee EmployeeID="Emp-6" LastName="Suyama" FirstName="Michael" Title="Sales Representative"/> </Northwind> |
<Northwind xmlns:sql="urn:schemas-microsoft-com:xml-sql"> <sql:header> <sql:param name="ID"/> </sql:header> <sql:xpath-query mapping-schema="listing4.xdr"> {{should this be "listing6.xdr"?}} /Customer/Order[@OrderID=$ID] </sql:xpath-query> </Northwind> |
<Northwind xmlns:sql="urn:schemas-microsoft-com:xml-sql"> <Order OrderID="Ord-10643" EmployeeID= "6" OrderDate="1997-08-25T00:00:00" RequiredDate= "1997-09-22T00:00:00" ShippedDate= "1997-09-02T00:00:00"> <Employee EmployeeID="Emp-6" LastName= "Suyama" FirstName="Michael" Title= "Sales Representative" /> <OrderDetail ProductID="Prod-28" UnitPrice= "45.6" Quantity="15"> <Discount>0.25</Discount> </OrderDetail> <OrderDetail ProductID="Prod-39" UnitPrice= "18" Quantity="21"> <Discount>0.25</Discount> </OrderDetail> <OrderDetail ProductID="Prod-46" UnitPrice= "12" Quantity="2"> <Discount>0.25</Discount> </OrderDetail> </Order> </Northwind> |
小结
通过使用上面我介绍的几种技术,我们可以直接从SQL Server 2000数据库中直接取得XML数据。如我所介绍,URL查询、XML模板文件、XDR架构和XPath查询提供了强大的功能,从SQL Server 2000中直接获得XML数据。除此之外,还有很多重要的概念,由于篇幅有限在本文中不可能详述,如FOR XML EXPLICIT查询和OPENXML这些技术,我会在以后的文章中进一步进行讨论,请大家等待。
本文转自http://dev.csdn.net/article/84813.shtm