微軟使得將ActiveX數據對象(ADO)的Recordset作為XML在ADO 2.1裡保存成為可能。這對於將Recordset保存在客戶計算機上用於脫機使用很有用處。如果你希望通過可擴展樣式表語言轉換(XSLT)來操控Recordset的數據,那麼這一能力也是很有用的。
要將Recordset作為XML來保存,就要調用Save方法,並將目標文件及adPersistXML標志進行傳遞:
Dim rst As Recordset
Set rst = New Recordset
rst.ActiveConnection = "PROVIDER=SQLOLEDB;DATA SOURCE=SQLServer;" & _
"Initial Catalog=pubs;Integrated Security=SSPI"
rst.CursorLocation = adUseClIEnt
rst.Open "Select * from authors where au_lname = 'Smith'"
rst.SaveApp.Path & "\recordset.xml", adPersistXML
rst.Close
如果所指定的文件命已經存在,那麼對Save方法的調用就會導致一個運行錯誤。
要打開已經保存的Recordset,就要將文件名傳遞給Recordset的Open方法。設置ActiveConnection屬性能夠讓你將Recordset保存會數據庫裡:
Dim rst As Recordset
Set rst = New Recordset
rst.CursorLocation = adUseClIEnt
rst.OpenApp.Path & "\recordset.XML"
rst.ActiveConnection = "PROVIDER=SQLOLEDB;DATA SOURCE=SQLServer;" & _
"Initial Catalog=pubs;Integrated Security=SSPI"
MsgBoxrst.RecordCount