SQLXML 3.0 是 SQL Server 2000 XML 支援的第三代版本,提供透過 HTTP、SOAP 存取資料庫、建立 Web 服務的完整中介層。核心元件為 ISAPI 擴充 sqlis3.dll,配合 MMC 管理單元設定 IIS 虛擬目錄。
Projects\Template、Projects\SOAP。Northwind),根目錄指向 Projects。template,型別選 template,路徑指向 Projects\Template。在 Projects\Template 建立 Customers.xml:
<ROOT xmlns:sql="urn:schemas-microsoft-com:xml-sql">
<SQL:QUERY client-side-xml="0">
SELECT * FROM Customers FOR XML AUTO
</SQL:QUERY>
</ROOT>
瀏覽器輸入 https://localhost/northwind/template/customers.xml 即可看到 XML 結果。
soapprocedures,型別 SOAP,目錄指向 Projects\SOAP。procedures (產生 procedures.ssc 與 procedures.wsdl)。SalesByCategory、CustOrderHist,方法名稱使用預設程序名。https://localhost/northwind/SOAP/procedures?wsdl。localhost。localhost.procedures ws = new localhost.procedures();
int returnValue;
object[] result = ws.CustOrderHist("ALFKI", out returnValue);
object[] 需轉型:成功為 XmlElement,錯誤為 SqlMessage。輔助方法:
public static XmlElement[] ProcessResult(object[] result) {
List<XmlElement> elements = new List<XmlElement>();
foreach (object o in result) {
if (o is XmlElement) elements.Add((XmlElement)o);
else if (o is SqlMessage) throw new Exception(((SqlMessage)o).Message);
}
return elements.ToArray();
}
DataSet 方便繫結 UI:
public static DataSet GetDataSetFromXmlFragment(XmlElement xml) {
DataSet ds = new DataSet();
XmlTextReader reader = new XmlTextReader(xml.OuterXml, XmlNodeType.Element, new XmlParserContext(null, null, null, XmlSpace.None));
ds.ReadXml(reader, XmlReadMode.InferSchema);
return ds;
}
設定方式同預存程序,方法對應型別選 template 或 SP (UDF 亦屬 SP 類)。範本 GetAllCustomers 可直接傳回 XML 結果供用戶端程式捕獲。
引用 MSXML 4.0 與 SoapClient 元件:
Dim sc As New SoapClient
sc.MSSoapInit "https://localhost/northwind/SOAP/procedures?wsdl"
Dim nodeList As IXMLDOMNodeList
Set nodeList = sc.CustOrderHist("ALFKI")
For Each node In nodeList
Debug.Print node.xml
Next
回傳為 IXMLDOMNodeList,需逐項迭代 IXMLDOMNode,配合 XSL 可美化輸出。
SQLXML 3.0 讓未導入 .NET 的環境也能輕鬆建立資料驅動 Web 服務,統一以 SOAP/HTTP 呼叫預存程序、範本與 UDF,大幅降低中介層開發門檻。最新版本請參考 MSDN XML 專區。