在查詢結尾加入 FOR XML AUTO (或 RAW, EXPLICIT) 即可將結果集直接轉為 XML 文件,大幅提升效率與開發速度。詳細語法請參閱 Books Online。
若繼續使用 Recordset 開啟命令,只能得到 Unicode XML Schema,無法取得 XML 實際內容。
Command.Properties("Output Stream") = ADO Stream 物件Command.Execute ,, 1024 (adExecuteStream)Stream.ReadText(-1)Dim adoConn, adoCmd, adoStream
Set adoConn = Server.CreateObject("ADODB.Connection")
Set adoCmd = Server.CreateObject("ADODB.Command")
Set adoStream = Server.CreateObject("ADODB.Stream")
adoConn.ConnectionString = "Provider=SQLOLEDB;Data Source=MyServer;Initial Catalog=MyDB;User Id=sa;Password=pwd;"
adoConn.Open
Set adoCmd.ActiveConnection = adoConn
adoCmd.CommandType = 1 ' adCmdText
adoCmd.CommandText = "SELECT * FROM Customers FOR XML AUTO, ROOT('Root')"
adoStream.Open
Set adoCmd.Properties("Output Stream") = adoStream
adoCmd.Execute ,, 1024 ' adExecuteStream
Response.Write adoStream.ReadText(-1)
adoStream.Close
Set adoStream = Nothing
adoConn.Close
Set adoCmd = Nothing
Set adoConn = Nothing
Input Stream 傳入 XML 文件執行 OPENXML 寫入資料庫。ReadText(chunkSize)。SELECT ... FOR XML PATH、TYPE 指令回傳 xml 型別,搭配 SqlXml 類別更高效。