Office中国论坛/Access中国论坛

标题: XML与ADODB.Recordset [打印本页]

作者: fan0217    时间: 2008-8-31 16:08
标题: XML与ADODB.Recordset

  1. Public Function ReadXml(xmlFile As String) As ADODB.Recordset
  2.     Dim rs As New ADODB.Recordset
  3.         rs.Open xmlFile, "Provider=MSPersist", , , adCmdFile
  4.     Set ReadXml = rs
  5.     Set rs = Nothing
  6. End Function
  7. Public Sub SaveXml(sql As String, xmlFile As String)
  8.     Dim conn As New ADODB.Connection
  9.     Dim rs As New ADODB.Recordset
  10.     Set conn = CurrentProject.Connection
  11.         rs.Open sql, conn, adOpenKeyset, adLockReadOnly
  12.         rs.Save xmlFile, adPersistXML
  13.     Set rs = Nothing
  14.     Set conn = Nothing
  15. End Sub
  16. Private Sub DelFile(xmlFile As String)
  17.     Dim fso
  18.         Set fso = CreateObject("Scripting.FileSystemObject")
  19.         If fso.FileExists(xmlFile) Then
  20.             fso.DeleteFile xmlFile
  21.         End If
  22.     Set fso = Nothing
  23. End Sub
  24. Sub ReadXmlTest()
  25.     Dim strXmlFile As String
  26.     Dim rs As New ADODB.Recordset
  27.    
  28.         strXmlFile = "C:\Test.xml"
  29.         Set rs = ReadXml(strXmlFile)
  30.         
  31.         Do While Not rs.EOF
  32.             Debug.Print rs("帐目编号")
  33.             rs.MoveNext
  34.         Loop
  35. End Sub
  36. Sub SaveXmlTest()
  37.     Dim strXmlFile As String, strSQL As String
  38.         strSQL = "SELECT * FROM 帐目;"
  39.         strXmlFile = "C:\Test.xml"
  40.         DelFile strXmlFile
  41.         SaveXml strSQL, strXmlFile
  42. End Sub


复制代码


用这样的方法,我们可以持久保存ADODB.Recordset,也可以替代临时表使用。
作者: tmtony    时间: 2008-8-31 22:40
这个是否也要引用xml
作者: fan0217    时间: 2008-9-1 09:06
原帖由 tmtony 于 2008-8-31 22:40 发表
这个是否也要引用xml


对,也需要引用xml




欢迎光临 Office中国论坛/Access中国论坛 (http://www.office-cn.net/) Powered by Discuz! X3.3