Clone 方法范例 (Visual Basic)

该范例使用 Clone 方法创建 Recordset 的副本并让用户独立地为每个副本的记录指针定位。

Public Sub CloneX()

   Dim arstStores(1 To 3) As ADODB.Recordset
   Dim intLoop As Integer
   Dim strSQL As String
   Dim strCnn As String
   Dim strMessage As String
   Dim strFind As String

   ' 将 SQL 语句和连接字符串赋值给变量。
   strSQL = "SELECT stor_name FROM Stores " & _
      "ORDER BY stor_name"
      strCnn = "Provider=sqloledb;" & _
      "Data Source=srv;Initial Catalog=pubs;User Id=sa;Password=; "

   ' 将记录集作为静态游标类型记录集打开。
   Set arstStores(1) = New ADODB.Recordset
   arstStores(1).CursorType = adOpenStatic
   arstStores(1).LockType = adLockBatchOptimistic
   arstStores(1).Open strSQL, strCnn, , , adCmdText

   ' 创建原始 Recordset 的两个副本。
   Set arstStores(2) = arstStores(1).Clone
   Set arstStores(3) = arstStores(1).Clone

   Do While True

      ' 在数组中循环以使用户每一遍都搜索相同 Recordset 的不同副本。
      For intLoop = 1 To 3

         ' 要求在显示每个 Recordset 当前记录集指针位置时搜索字符串。
         strMessage = _
            "Recordsets from stores table:" & vbCr & _
            "  1 - Original - Record pointer at " & _
            arstStores(1)!stor_name & vbCr & _
            "  2 - Clone - Record pointer at " & _
            arstStores(2)!stor_name & vbCr & _
            "  3 - Clone - Record pointer at " & _
            arstStores(3)!stor_name & vbCr & _
            "Enter search string for #" & intLoop & ":"
         strFind = Trim(InputBox(strMessage))
         If strFind = "" Then Exit Do

         ' 查找搜索字符串,如果没有匹配的,请跳转到最后的记录。
         arstStores(intLoop).Filter = "stor_name >= '" & strFind & "'"
         If arstStores(intLoop).EOF Then
            arstStores(intLoop).Filter = adFilterNone
            arstStores(intLoop).MoveLast
         End If

      Next intLoop

   Loop

   arstStores(1).Close
   arstStores(2).Close
   arstStores(3).Close

End Sub

VBScript 版本

下面是使用 VBScript 编写、并用于 Active Server Page (ASP) 的相同范例。如需查看该完整功能范例,请使用与 IIS 一同安装并位于 C:\InetPub\ASPSamp\AdvWorks 的数据源 AdvWorks.mdb,来创建名为 AdvWorks 的系统“数据源名称”(DSN)。这是 Microsoft Access 数据库文件。请使用“查找”命令定位文件 Adovbs.inc,并将其放入计划使用的目录中。请将以下代码剪切并粘贴到“记事本”或其他文本编辑器中,另存为“Clone.asp”。这样,便可在任何客户端浏览器中查看结果。

如要执行该范例,请将行 RsCustomerList.Source = "Customers" 改为 RsCustomerList.Source = "Products" 以便为更大的表计数。

<!-- #Include file="ADOVBS.INC" -->
<% Language = VBScript %>
<HTML><HEAD>
<TITLE>ADO Clone Method</TITLE>
</HEAD><BODY> <Center>
<H3>ADO Clone Method</H3>
<!--- A用于创建记录集的 ADO Connection 对象 -->
<% 
' 创建并打开 Connection 对象。
Set OBJdbConnection = Server.CreateObject("ADODB.Connection") 
OBJdbConnection.Open "AdvWorks" 
' 创建并打开 Recordset 对象。
Set RsCustomerList = Server.CreateObject("ADODB.Recordset")
RsCustomerList.ActiveConnection = OBJdbConnection
RsCustomerList.CursorType = adOpenKeyset
RsCustomerList.LockType = adLockOptimistic
RsCustomerList.Source = "Customers"
RsCustomerList.Open
%>
<HR>
<!-- 在 Customers 表中循环,每循环一次将 Counter 变量加 1 -->
<% 
   Set MyRecordset = RSCustomerList.Clone
   Counter = 0
   Do Until MyRecordset.EOF
      Counter = Counter + 1
      MyRecordset.MoveNext
   Loop
%>
<!-- 显示结果 -->
<H3>There Are <%=Counter %> Records in the Customers Table</H3>
<BR><HR>
<H4>Location of DSN Datbase</H4>
<%' Show location of DSN data source
Response.Write(OBJdbConnection)
%>
<HR></Center></BODY></HTML>