VB6,VBA保存ADO记录集并重新加载更新adPersistADTG

便携式计算(例如,使用笔记本电脑)催生了对可以同时在连接和断开状态下运行的应用程序的需求。 ADO 增加了这方面的支持,使开发人员能够将客户端游标 Recordset 保存到磁盘并稍后重新加载。

有多种场景可以使用这种类型的功能,包括:

  • 旅行:在路上使用应用程序时,提供更改和添加新记录的功能至关重要,这些记录随后可以重新连接到数据库并提交。

  • 不经常更新的查找:在应用程序中,通常使用表作为查找,例如,州税表。 它们不经常更新,并且是只读的。 无需在每次应用程序启动时从服务器重新读取数据,应用程序只需从本地持久保存的 Recordset 加载数据。

在 ADO 中,若要保存和加载 Recordets,请在 ADO Recordset 对象上使用 Recordset.Save 和 Recordset.Open(,,,,adCmdFile) 方法。

可以使用 Recordset Save 方法将 ADO Recordset 保存到磁盘上的文件中。 (还可以将 Recordset 保存到 ADO Stream 对象。Stream 对象稍后将在本指南中讨论)稍后,可以使用 Open 方法在准备使用 Recordset 时重新打开它。 默认情况下,ADO 将 Recordset 保存为专有的 Microsoft Advanced Data TableGram (ADTG) 格式。 此二进制格式使用 adPersistADTG PersistFormatEnum 值指定。 或者,可以选择使用 adPersistXML 将 Recordset 保存为 XML。 有关将 Recordsets 保存为 XML 的详细信息,请参阅以 XML 格式保存记录。

Save 方法的语法如下:

复制

  
recordset.  
Save  
Destination, PersistFormat  
  

首次保存 Recordset 时,可以选择指定 Destination。 如果省略 Destination,则会创建一个新文件,其名称设置为 Recordset 的 Source 属性的值。

在首次保存后调用 Save 时省略 Destination,否则将发生运行时错误。 如果随后使用新 Destination 调用 Save,则 Recordset 将保存到新目标。 但是,新目标和原始目标都将打开。

Save 不会关闭 Recordset 或 Destination,因此可以继续使用 Recordset 并保存最近的更改。 Destination 保持打开状态,直到 Recordset 关闭,在此期间,其他应用程序可以读取但不写入 Destination。

出于安全考虑,Save 方法只允许使用 Microsoft Internet Explorer 执行的脚本中的低安全设置和自定义安全设置。

当异步 Recordset 提取、执行或更新操作正在进行时,如果调用 Save 方法,则 Save 将等待异步操作完成。

记录从 Recordset 的第一行开始保存。 Save 方法完成后,当前行位置将移动到 Recordset 的第一行。

为了获得最佳结果,请使用 Save 将 CursorLocation 属性设置为 adUseClient。 如果提供程序不支持保存 Recordset 对象所需的全部功能,游标服务将提供该功能。

当持久保存 Recordset,并且 CursorLocation 属性被设置为 adUseServer 时,Recordset 的更新功能会受限制。 通常,仅允许单表更新、插入和删除(取决于提供程序功能)。 Resync 方法在此配置中也不可用。

由于 Destination 参数可以接受任何支持 OLE DB IStream 接口的对象,因此可以将 Recordset 直接保存到 ASP Response 对象中。

在以下示例中,Save 和 Open 方法用于保存 Recordset 并稍后重新打开它


'BeginPersist  
   conn.ConnectionString = _  
   "Provider=SQLOLEDB;Data Source=MySQLServer;" _  
      & "Integrated Security=SSPI;Initial Catalog=pubs"  
   conn.Open  
  
   conn.Execute "create table testtable (dbkey int " & _  
      "primary key, field1 char(10))"  
   conn.Execute "insert into testtable values (1, 'string1')"  
  
   Set rst.ActiveConnection = conn  
   rst.CursorLocation = adUseClient  
  
   rst.Open "select * from testtable", conn, adOpenStatic, _  
      adLockBatchOptimistic  
  
   'Change the row on the client  
   rst!field1 = "NewValue"  
  
   'Save to a file--the .dat extension is an example; choose  
   'your own extension. The changes will be saved in the file  
   'as well as the original data.  
   MyFile = Dir("c:\temp\temptbl.dat")  
   If MyFile <> "" Then  
       Kill "c:\temp\temptbl.dat"  
   End If  
  
   rst.Save "c:\temp\temptbl.dat", adPersistADTG  
   Set rst = Nothing  
  
   'Now reload the data from the file  
   Set rst = New ADODB.Recordset  
   rst.Open "c:\temp\temptbl.dat", , adOpenStatic, _  
      adLockBatchOptimistic, adCmdFile  
  
   Debug.Print "After Loading the file from disk"  
   Debug.Print "   Current Edited Value: " & rst!field1.Value  
   Debug.Print "   Value Before Editing: " & rst!field1.OriginalValue  
  
   'Note that you can reconnect to a connection and  
   'submit the changes to the data source  
   Set rst.ActiveConnection = conn  
   rst.UpdateBatch  
'EndPersist

你可能感兴趣的:(开发语言,数据库,VB6,ado)