我们有一个带有MS ACCESS 2016 Front的SQL Server后端。我们有一个通用的ADO连接对象,可用于将SQL命令发送到数据库。当我们有返回结果的SQL时,我们正在使用代码来构建passthru查询对象,这是将SQL输出输出到前端的一种简便方法。
我们现在处在这样的情况下:我们有一些非常复杂的SQL返回一个输出,但是包含几个语句,包括在服务器上构造多个临时表。我们不能使用“构建通过查询对象”方法,因为passthrus无法处理多个SQL语句,并且由于查询完成后临时表会被破坏,因此我们一次不能发送一个SQL语句。>
目前,我们现在使用ADO对象将输出获取到ADO记录集中,然后将记录集保存为.XML,然后最终将.XML文件导入数据库中。但这是很多I / O,对于大输出来说,它会变慢。
是否有更好(更快)的方法来将复杂SQL的输出输出到MS Access Frontend中的本地表中?
以下是我们的vba类对象的摘要:
Private Const DB_CONNECTION_STRING As String = "<our connection string>"
Private conn As ADODB.Connection
Private cmd As ADODB.Command
Private recAff As Long
______________________________________________________________
Private Sub Class_Initialize()
Set conn = New ADODB.Connection
Set cmd = New ADODB.Command
conn.Open DB_CONNECTION_STRING
With cmd
.ActiveConnection = conn
.CommandTimeout = 0
End With
End Sub
...
<more properties/methods>
...
Public Sub LocalizeOutput(fetchSQL As String, InputTableName As String)
'If the InputTableName does not exist, a new table will be created. All fields will be datatyped as Text
'Otherwise, if the InputTableName does exist, data will be appended. Caller is responsible for handling errors
Dim rs As ADODB.Recordset
Dim XMLFilePath As String
Dim domIn As Object
Dim domOut As Object
Dim domStylesheet As Object
XMLFilePath = Environ("TEMP") & "\TableImport.xml"
fetchSQL = "SET NOCOUNT ON; " & vbNewLine & fetchSQL
Set domIn = CreateObject("Microsoft.XMLDOM")
domIn.async = False
'Execute the query
With cmd
.CommandType = adCmdText
.CommandText = fetchSQL
Set rs = .Execute(recAff)
End With
Dim tbl As TableDef
'Save off the output as xml file
If FileExists(XMLFilePath) Then
Call FileDelete(XMLFilePath)
End If
rs.Save XMLFilePath, adPersistXML
'Write the XML Transformation File
Call WriteXSLTransform(InputTableName)
'Convert the ado_xml file into an Access readable xml file
domIn.Load XMLFilePath
Set domStylesheet = CreateObject("Microsoft.XMLDOM")
domStylesheet.Load Environ("TEMP") & "\ADOXMLToAccess.xsl"
Set domOut = CreateObject("Microsoft.XMLDOM")
domIn.transformNodeToObject domStylesheet, domOut
'Set domIn = Nothing
Call FileDelete(XMLFilePath)
'Save the output
domOut.Save XMLFilePath
'Import the saved document into Access
Application.ImportXML XMLFilePath, acAppendData
'clean up
Call FileDelete(XMLFilePath)
Call FileDelete(Environ("TEMP") & "\ADOXMLToAccess.xsl")
Set rs = Nothing
Set domIn = Nothing
Set domOut = Nothing
Set domStylesheet = Nothing
End Sub