我正在尝试从ASP中返回查询结果,当我使用像Response.write Recordset(0)之类的东西时,它正在工作,但是它不能以xml格式工作。这是我的代码:
Dim conn
Dim Recordset
Dim xmlDoc
Set conn = Server.CreateObject("ADODB.Connection")
Set Recordset = Server.CreateObject("ADODB.Recordset")
SQL = "SELECT * FROM dbo.myTable for xml auto;"
conn.Open "Provider=SQLOLEDB; Data Source = myPC\SQLEXPRESS;
Initial Catalog = peopleDatabase; User Id = rafalsonn; Password=xxx"
Recordset.Open SQL,conn
Set xmlDoc = Server.CreateObject("Microsoft.XMLDOM")
Recordset.Save xmlDoc,1
Recordset.Close
Set Recordset=nothing
conn.Close
Set conn=nothing
Response.Write xmlDoc.xml
结果:
此XML文件似乎没有任何关联的样式信息 用它。文档树如下所示。
我试图让它工作很长时间,但我不知道bug在哪里。问候,Rafał
答案 0 :(得分:3)
以下是我如何实现这个目标
<%
Set oXML = Server.CreateObject("MSXML2.DomDocument.6.0")
Set oCmd = Server.CreateObject("ADODB.Command")
oCmd.ActiveConnection = "Provider=SQLOLEDB;Data Source=localhost\SQLEXPRESS;User ID=myuserid;Initial Catalog=mydatabase;Password=mypassword"
oCmd.Properties("Output Stream") = oXML
oXml.async = false
oCmd.CommandText = "SELECT id, Headline, Author, Publication, Pubdate, Intro from Articles as Article for xml auto, elements, root('Articles')"
oCmd.Execute , , 1025
Response.ContentType="text/xml"
Response.Write("<?xml version='1.0' encoding='utf-8'?>")
Response.Write oXML.xml
Set oXML = Nothing
Set oCmd = Nothing
%>
'root('Articles')'将xml输出放在<Articles>...</Articles>
此MSDN页面显示“元素”的作用 https://msdn.microsoft.com/en-us/library/ms188273.aspx
答案 1 :(得分:3)
这是另一种方式。
在ADO XML Persistence Format中将Recordset的XML输出直接打印到Response。
<%
Response.ContentType = "text/xml"
Const adPersistXML = 1
Dim conn
Dim Recordset
Set conn = Server.CreateObject("ADODB.Connection")
Set Recordset = Server.CreateObject("ADODB.Recordset")
SQL = "SELECT * FROM dbo.myTable"
conn.Open "Provider=SQLOLEDB; Data Source = myPC\SQLEXPRESS; Initial Catalog = peopleDatabase; User Id = rafalsonn; Password=xxx"
Recordset.Open SQL,conn, 1, 1
Recordset.Save Response, adPersistXML
%>
答案 2 :(得分:1)
将CursorLocation和CursorType设置为:
<%@ Page Language="VB" AspCompat="true" %>
<%
Dim rs as Object = Server.CreateObject("ADODB.Recordset")
Dim cn as Object = Server.CreateObject("ADODB.Connection")
Dim xml as Object = Server.CreateObject("MSXML2.DomDocument.6.0")
Dim sql as String = "select * from table1 where rownum < 10"
cn.Open ("Provider=OraOLEDB.Oracle;Data Source=server1/inst1;User ID=user1;Password=password1;")
rs.CursorLocation = 3 'adUseClient
rs.CursorType = 3 'adOpenStatic
rs.LockType = 1 'adLockReadOnly
rs.Open (sql, cn)
rs.Save (xml,1)
Response.ContentType="text/xml"
Response.Write("<?xml version='1.0' encoding='utf-8'?>")
Response.Write(xml.xml )
cn.Close
%>