MS Access VBA-从存储过程输出中下载XML文件

时间:2018-08-17 06:19:32

标签: xml ms-access stored-procedures access-vba

我需要具有一个从存储过程中下载XML文件并将其保存到磁盘的功能。 MS Access VBA是否可以?

这是我在SQL Server中的测试示例:

CREATE TABLE [dbo].[TestXML](
[ID] [int] NOT NULL,
[PID] [int] NULL,
[Code] [int] NULL,
[Col1] [int] NULL,
[Col2] [int] NULL,
[Col3] [decimal](6, 2) NULL,
[Col4] [decimal](6, 2) NULL,
[Col5] [int] NULL,
CONSTRAINT [PK_TestXML] PRIMARY KEY CLUSTERED ([ID] ASC)
)

GO
INSERT [dbo].[TestXML] ([ID], [PID], [Code], [Col1], [Col2], [Col3], [Col4], [Col5]) VALUES (1, 1000, 10, 1, 2, CAST(0.20 AS Decimal(6, 2)), CAST(0.10 AS Decimal(6, 2)), 1)
INSERT [dbo].[TestXML] ([ID], [PID], [Code], [Col1], [Col2], [Col3], [Col4], [Col5]) VALUES (2, 1000, 20, NULL, 1, CAST(1.00 AS Decimal(6, 2)), CAST(1.00 AS Decimal(6, 2)), 1)
INSERT [dbo].[TestXML] ([ID], [PID], [Code], [Col1], [Col2], [Col3], [Col4], [Col5]) VALUES (3, 1000, 30, NULL, NULL, CAST(2.00 AS Decimal(6, 2)), CAST(2.00 AS Decimal(6, 2)), 5)
GO

过程

CREATE PROCEDURE returnXML 
(@ID int,
 @xmlOut XML OUTPUT)

AS
BEGIN

SET NOCOUNT ON;

SELECT @xmlOut=(SELECT PID [p/@v],(
    SELECT Code as [code/@c], Col1 AS [code/val]
    FROM [dbo].[TestXML]
    WHERE ID=@ID
    FOR XML PATH(''),TYPE)
  FROM [dbo].[TestXML]
  WHERE ID=@ID
  FOR XML PATH('u'),TYPE)

END
GO

VBA(我试图做的事,但是我不知道该如何使用XML变量)

Function getXML(sproc As String, id As Integer) As Object
Dim cnn As New ADODB.Connection
    Dim rst As New ADODB.Recordset
    Dim cmd As ADODB.Command
    Dim cnnStr As String
    Dim Rs As New ADODB.Recordset
    Dim StrSproc As String

    Set cnn = New ADODB.Connection
    Set rst = New ADODB.Recordset
    Set cmd = New ADODB.Command
    Set Rs = New ADODB.Recordset


    cnnStr = getConnString()

    With cnn
        .CommandTimeout = 900
        .ConnectionString = cnnStr
        .Open
    End With
    With cmd
        .ActiveConnection = cnn
        .CommandType = adCmdStoredProc
        .CommandText = sproc
        .Parameters.Append .CreateParameter("@ID", adInteger, adParamInput, id)
        .Parameters.Append .CreateParameter("@xmlOut", adLongVarChar, adParamOutput)

    End With
    With Rs
        .CursorType = adOpenStatic
        .CursorLocation = adUseClient
        .LockType = adLockOptimistic
        .Open cmd
    End With
    Set rst = cmd.Execute(, , adCmdStoredProc)
    getXML = cmd.Parameters("@xmlOut").Value

End Function

任何帮助将不胜感激,谢谢!

1 个答案:

答案 0 :(得分:3)

您真的必须使用T-SQL返回参数吗?

通常,如果您编写“其他” T-SQL例程并希望返回一个值,则存储过程的返回参数是最佳的。

对于一般代码,为什么不执行select并将值返回为表,而是像大多数情况下那样进行选择呢?

因此代替

Select @MyXml = (blab la bla)

随便走

select (blab la bla).

或者最后,简单地将@MyXml声明为var,然后开始

Select @MyXml

如果您走上述道路,那么您的存储过程将变为:

CREATE PROCEDURE returnXML 
    (@ID INT)
AS
BEGIN
    SET NOCOUNT ON;

    SELECT (SELECT PID [p/@v],(
    SELECT Code as [code/@c], Col1 AS [code/val]
    FROM [dbo].[TestXML]
    WHERE ID=@ID
    FOR XML PATH(''),TYPE)
FROM [dbo].[TestXML]
WHERE ID=@ID
FOR XML PATH('u'),TYPE)
END
GO

在Access中,如果您声明了一个传递查询(您应该有一个“通用”查询,那么您可以执行以下操作:

Sub Test1()

   Dim i        As Integer
   Dim strXML   As String

   i = InputBox("What id")
       With CurrentDb.QueryDefs("qryPassR")
      .SQL = "returnXML " & i
      strXML = .OpenRecordset()(0)
   End With

   Debug.Print strXML

在Access中使用pt查询意味着您不会弄乱代码中的连接字符串。

因此,除非存在某些“大”原因要使用该返回参数,否则可以使用上面的方法。而且,如果您真的必须使用一些“现有的” t-sql例程来返回一个值,那么我建议使用exec + select @MyXml。

因此,即使您被迫使用返回值,您在上面的.SQL =中也将执行以下操作:

declare @MyXML varcharmx
Myxml 1,@Mxml;select @myxml