如何使用访问VBA将SQL输出存储为XML

时间:2014-10-24 20:15:07

标签: vba access-vba

我对描述道歉,我不知道如何正确命名

我有一些存储过程(在SQL2000上运行),它提供XML数据作为输出。这一切都正常,正如预期的那样,我在我的DTS中使用VB模块来导出和存储数据。此文件的简化版本如下

set objStream   = CreateObject( "ADODB.Stream" ) 
set objXML  = CreateObject( "MSXML2.DOMDocument" ) 

set objConn = CreateObject( "ADODB.Connection" ) 
objConn.ConnectionTimeout = 3600
objConn.CommandTimeOut = 0
ObjConn.open(DTSGlobalVariables("ConnStringConso").Value)

set objComm          = CreateObject( "ADODB.Command" )  
objComm.ActiveConnection = objConn 
objComm.CommandType      = adCmdStoredProc

'Create the Stream object
set objStream = CreateObject( "ADODB.Stream" ) 
objStream.Open  
objComm.Properties( "Output Stream" ) = objStream       

' Execute the command

objComm.CommandText = "my_stored_procedure"     
objComm.CommandTimeout = 3600
objComm.Execute ,, adExecuteStream 

' Read the info returned adding the header and footer info
objStream.Position = 0 
objXML.LoadXML("<?xml version='1.0'?>" & objStream.ReadText) 

' Create the output XML file
xmlfile = filePath & "myfile.xml"
objXML.save( xmlfile )

所以基本上这样做是调用过程,将输出存储在流中并保存文件,相当简单直到这里没问题。 但是,我还需要能够通过VBA从我的访问前端执行此操作。因此,调用该过程,包装内容并将其保存为XML文件。不幸的是,我的VBA知识有点生疏......

这就是我目前所拥有的:

Dim cnn As ADODB.Connection
Dim cmd As New ADODB.Command, rs As New ADODB.Recordset

Set cnn = New ADODB.Connection
cnn.ConnectionString = "DRIVER=SQL Server;SERVER=myserver;DATABASE=myDatabase;uid=myID;pwd=myPW;Trusted_Connection=Yes"
cnn.Open cnn.ConnectionString

Set cmd = New ADODB.Command
With cmd
.ActiveConnection = cnn
.CommandType = adCmdStoredProc
.CommandText = "my_stored_procedure"
End With

rs.CursorType = adOpenStatic
rs.CursorLocation = adUseClient
rs.LockType = adLockOptimistic
rs.Open cmd

Debug.Print rs(0)

到目前为止这么好,我可以打开程序,我得到数据返回,但那是我的运气结束的地方。返回的数据似乎仅限于一定数量的字符(大约2000),我正在努力保存它。

我在下面尝试过快速而肮脏的测试,但它似乎搞砸了我的XML(我的所有属性都突然被双重引用)并且如上所述,只有一部分内容被导出,所以有关如何做的任何建议它会得到高度赞赏

Dim myFile As String
myFile = "d:\output.xml"
Open myFile For Output As #1
Write #1, rs(0)
Close #1

1 个答案:

答案 0 :(得分:0)

没关系,我在此期间发现了自己。我有2个问题,VBA ADODB与VB变体的工作方式不同,而且我在SQL查询中使用FOR XML时,输出显然是在不同的行中被切断。因此,为什么我只有部分输出。在VBA中摆弄流媒体部分并浏览所有记录使其工作。不太确定这是最优化的方式,所以如果有方法可以改进,我有兴趣知道。

Dim cmd As New ADODB.Command, rs As New ADODB.Recordset
Dim cnn As ADODB.Connection
Set cnn = New ADODB.Connection
cnn.ConnectionString = "DRIVER=SQL Server;SERVER=myServer;DATABASE=myDB;uid=myID;pwd=myPW;Trusted_Connection=Yes"
cnn.Open cnn.ConnectionString

Set cmd = New ADODB.Command
With cmd
    .ActiveConnection = cnn
    .CommandType = adCmdStoredProc
    .CommandText = myProc
End With

rs.CursorType = adOpenStatic
rs.CursorLocation = adUseClient
rs.LockType = adLockOptimistic
rs.Open cmd

Dim myXML As Variant

If Not (rs.EOF And rs.BOF) Then
    rs.MoveFirst
    Do Until rs.EOF = True
        myXML = myXML & rs.Fields(0)
        rs.MoveNext
    Loop
End If

'Create the Stream object
Set objStream = CreateObject("ADODB.Stream")
With objStream
    .Type = 2 'Text
    .Charset = "utf-8"
    .Open
    .WriteText myXML
    .SaveToFile (filePath & myXMLFile)
    .Close

End With