此頁(yè)沒(méi)有內(nèi)容條目
內(nèi)容

Clone 方法范例 (Visual Basic)

該范例使用 Clone 方法創(chuàng)建 Recordset 的副本并讓用戶(hù)獨(dú)立地為每個(gè)副本的記錄指針定位。

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 語(yǔ)句和連接字符串賦值給變量。

   strSQL = "SELECT stor_name FROM Stores " & _

      "ORDER BY stor_name"

      strCnn = "Provider=sqloledb;" & _

      "Data Source=srv;Initial Catalog=pubs;User Id=sa;Password=; "

   ' 將記錄集作為靜態(tài)游標(biāo)類(lèi)型記錄集打開(kāi)。

   Set arstStores(1) = New ADODB.Recordset

   arstStores(1).CursorType = adOpenStatic

   arstStores(1).LockType = adLockBatchOptimistic

   arstStores(1).Open strSQL, strCnn, , , adCmdText

   ' 創(chuàng)建原始 Recordset 的兩個(gè)副本。

   Set arstStores(2) = arstStores(1).Clone

   Set arstStores(3) = arstStores(1).Clone

   Do While True

      ' 在數(shù)組中循環(huán)以使用戶(hù)每一遍都搜索相同 Recordset 的不同副本。

      For intLoop = 1 To 3

         ' 要求在顯示每個(gè) Recordset 當(dāng)前記錄集指針位置時(shí)搜索字符串。

         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

         ' 查找搜索字符串,如果沒(méi)有匹配的,請(qǐng)?zhí)D(zhuǎn)到最后的記錄。

         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 編寫(xiě)、并用于 Active Server Page (ASP) 的相同范例。如需查看該完整功能范例,請(qǐng)使用與 IIS 一同安裝并位于 C:\InetPub\ASPSamp\AdvWorks 的數(shù)據(jù)源 AdvWorks.mdb,來(lái)創(chuàng)建名為 AdvWorks 的系統(tǒng)“數(shù)據(jù)源名稱(chēng)”(DSN)。這是 Microsoft Access 數(shù)據(jù)庫(kù)文件。請(qǐng)使用“查找”命令定位文件 Adovbs.inc,并將其放入計(jì)劃使用的目錄中。請(qǐng)將以下代碼剪切并粘貼到“記事本”或其他文本編輯器中,另存為“Clone.asp”。這樣,便可在任何客戶(hù)端瀏覽器中查看結(jié)果。

如要執(zhí)行該范例,請(qǐng)將行 RsCustomerList.Source = "Customers" 改為 RsCustomerList.Source = "Products" 以便為更大的表計(jì)數(shù)。

<!-- #Include file="ADOVBS.INC" -->

<% Language = VBScript %>

<HTML><HEAD>

<TITLE>ADO Clone Method</TITLE>

</HEAD><BODY> <Center>

<H3>ADO Clone Method</H3>

<!--- A用于創(chuàng)建記錄集的 ADO Connection 對(duì)象 -->

<%

' 創(chuàng)建并打開(kāi) Connection 對(duì)象。

Set OBJdbConnection = Server.CreateObject("ADODB.Connection")

OBJdbConnection.Open "AdvWorks"

' 創(chuàng)建并打開(kāi) Recordset 對(duì)象。

Set RsCustomerList = Server.CreateObject("ADODB.Recordset")

RsCustomerList.ActiveConnection = OBJdbConnection

RsCustomerList.CursorType = adOpenKeyset

RsCustomerList.LockType = adLockOptimistic

RsCustomerList.Source = "Customers"

RsCustomerList.Open

%>

<HR>

<!-- 在 Customers 表中循環(huán),每循環(huán)一次將 Counter 變量加 1 -->

<%

   Set MyRecordset = RSCustomerList.Clone

   Counter = 0

   Do Until MyRecordset.EOF

      Counter = Counter + 1

      MyRecordset.MoveNext

   Loop

%>

<!-- 顯示結(jié)果 -->

<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>