⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mdmthmovefirstx.htm

📁 ADO使用手册,非常详细
💻 HTM
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<HTML dir=ltr>
<HEAD>
<META HTTP-EQUIV="Content-Type" Content="text/html; charset=gb2312"><title>MoveFirst、MoveLast、MoveNext 和 MovePrevious 方法范例</title>
<style>@import url(msdn_ie4.css);</style>
</HEAD>
<BODY>
<h3><a name="mdmthmovefirstx"></a>MoveFirst、MoveLast、MoveNext 和 MovePrevious 方法范例</h3>
<p>
该范例使用 <b>MoveFirst</b>、<b>MoveLast</b>、<b>MoveNext</b> 以及 <b>MovePrevious</b> 方法,按照所提供的命令移动 <b>Recordset</b> 的记录指针。运行该过程需要 MoveAny 过程。</p>
<pre>Public Sub MoveFirstX()   Dim rstAuthors As ADODB.Recordset
   Dim strCnn As String
   Dim strMessage As String
   Dim intCommand As Integer   ' 打开 Authors 表的记录集。
      strCnn = "Provider=sqloledb;" &amp; _
      "Data Source=srv;Initial Catalog=pubs;User Id=sa;Password=; "
   Set rstAuthors = New ADODB.Recordset
   rstAuthors.CursorType = adOpenStatic
   ' 使用客户端游标激活 AbsolutePosition 属性。
   rstAuthors.CursorLocation = adUseClient
   rstAuthors.Open "authors", strCnn, , , adCmdTable   ' 显示当前记录信息并获得用户的方法选择。
   Do While True      strMessage = "Name: " &amp; rstAuthors!au_fName &amp; " " &amp; _
         rstAuthors!au_lName &amp; vbCr &amp; "Record " &amp; _
         rstAuthors.AbsolutePosition &amp; " of " &amp; _
         rstAuthors.RecordCount &amp; vbCr &amp; vbCr &amp; _
         "[1 - MoveFirst, 2 - MoveLast, " &amp; vbCr &amp; _
         "3 - MoveNext, 4 - MovePrevious]"
      intCommand = Val(Left(InputBox(strMessage), 1))
      If intCommand &lt; 1 Or intCommand &gt; 4 Then Exit Do      ' 根据用户输入调用方法。
      MoveAny intCommand, rstAuthors
   Loop
   rstAuthors.CloseEnd SubPublic Sub MoveAny(intChoice As Integer, _
   rstTemp As Recordset)   ' 使用指定方法捕获 BOF 和 EOF。
   Select Case intChoice
      Case 1
         rstTemp.MoveFirst
      Case 2
         rstTemp.MoveLast
      Case 3
         rstTemp.MoveNext
         If rstTemp.EOF Then
            MsgBox "Already at end of recordset!"
            rstTemp.MoveLast
         End If
      Case 4
         rstTemp.MovePrevious
         If rstTemp.BOF Then
            MsgBox "Already at beginning of recordset!"
            rstTemp.MoveFirst
         End If
   End SelectEnd Sub
</pre>
<p class=label>
<b>VBScript 版本</b></p>
<p>
下面是使用 VBScript 编写、并用于 Active Server Page (ASP) 的相同范例。如需查看该完整功能范例,请使用与 IIS 一同安装并位于 C:\InetPub\ASPSamp\AdvWorks 的数据源 AdvWorks.mdb,来创建名为 AdvWorks 的系统“数据源名称”(DSN)。这是 Microsoft Access 数据库文件。请使用查找命令定位文件 Adovbs.inc,并将其放入计划使用的目录中。请将以下代码剪切并粘贴到记事本或其他文本编辑器中,另存为“MoveOne.asp”。这样,便可在任何客户端浏览器中查看结果。</p>
<p>
可尝试在记录集的上限和下限之外移动以查看错误处理的运作。</p>
<pre>&lt;!-- #Include file="ADOVBS.INC" --&gt;
&lt;% Language = VBScript %&gt;
&lt;HTML&gt;&lt;HEAD&gt;
&lt;TITLE&gt;ADO MoveNext MovePrevious MoveLast MoveFirst Methods&lt;/TITLE&gt;&lt;/HEAD&gt;
&lt;BODY&gt; 
&lt;FONT FACE="MS SANS SERIF" SIZE=2&gt;
&lt;Center&gt;
&lt;H3&gt;ADO Methods&lt;BR&gt;MoveNext MovePrevious MoveLast MoveFirst&lt;/H3&gt;
&lt;!-- 在服务器上创建 Connection 和 Recordset 对象 --&gt;
&lt;%
 ' 创建并打开 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' 检查 Request.Form 集合以查看所记录的任何移动。If Not IsEmpty(Request.Form("MoveAmount")) Then
' 跟踪该会话的移动数目和方向。
   
   Session("Moves") = Session("Moves") + Request.Form("MoveAmount")
   
   Clicks = Session("Moves")
' 移动到上一个已知位置。
   RsCustomerList.Move CInt(Clicks)
' 检查移动为 + 还是 - 并进行错误检查。
      If CInt(Request.Form("MoveAmount")) = 1 Then
      
         If RsCustomerList.EOF Then
            Session("Moves") = RsCustomerList.RecordCount
            RsCustomerList.MoveLast
         End If            RsCustomerList.MoveNext
      End If      If Request.Form("MoveAmount") &lt; 1 Then
         
         RsCustomerList.MovePrevious
      End If
' 检查有无单击 First Record 或 Last Record 命令按钮。
      If Request.Form("MoveLast") = 3 Then
         RsCustomerList.MoveLast
         Session("Moves") = RsCustomerList.RecordCount
      End If
      If Request.Form("MoveFirst") = 2 Then
         RsCustomerList.MoveFirst
         Session("Moves") = 1
      End If
   
   
End If
' 对 Move Button 单击组合进行错误检查。
      If RsCustomerList.EOF Then
         Session("Moves") = RsCustomerList.RecordCount
         RsCustomerList.MoveLast
         Response.Write "This is the Last Record"
         End If   
   
      If RsCustomerList.BOF Then
         Session("Moves") = 1
         RsCustomerList.MoveFirst
         Response.Write "This is the First Record"
      End If
   %&gt;&lt;H3&gt;Current Record Number is &lt;BR&gt;
&lt;!-- 显示当前记录数目和记录集大小 --&gt;
&lt;% If IsEmpty(Session("Moves"))&nbsp; Then 
Session("Moves") = 1
End If
%&gt;&lt;%Response.Write(Session("Moves") )%&gt; of &lt;%=RsCustomerList.RecordCount%&gt;&lt;/H3&gt;
&lt;HR&gt;
&lt;Center&gt;&lt;TABLE COLSPAN=8 CELLPADDING=5 BORDER=0&gt;&lt;!-- Customer 表的 BEGIN 列标头行 --&gt;&lt;TR&gt;&lt;TD ALIGN=CENTER BGCOLOR="#008080"&gt;
&lt;FONT STYLE="ARIAL NARROW" COLOR="#ffffff" SIZE=1&gt;Company Name&lt;/FONT&gt;
&lt;/TD&gt;
&lt;TD ALIGN=CENTER BGCOLOR="#008080"&gt;
&lt;FONT STYLE="ARIAL NARROW" COLOR="#ffffff" SIZE=1&gt;Contact Name&lt;/FONT&gt;
&lt;/TD&gt;
&lt;TD ALIGN=CENTER WIDTH=150 BGCOLOR="#008080"&gt;
&lt;FONT STYLE="ARIAL NARROW" COLOR="#ffffff" SIZE=1&gt;Phone Number&lt;/FONT&gt;
&lt;/TD&gt;
&lt;TD ALIGN=CENTER BGCOLOR="#008080"&gt;
&lt;FONT STYLE="ARIAL NARROW" COLOR="#ffffff" SIZE=1&gt;City&lt;/FONT&gt;
&lt;/TD&gt;
&lt;TD ALIGN=CENTER BGCOLOR="#008080"&gt;
&lt;FONT STYLE="ARIAL NARROW" COLOR="#ffffff" SIZE=1&gt;State/Province&lt;/FONT&gt;
&lt;/TD&gt;&lt;/TR&gt;&lt;!-- 显示 Customer 表的 ADO 数据 --&gt;  &lt;TR&gt;
  &lt;TD BGCOLOR="f7efde" ALIGN=CENTER&gt; 
  &lt;FONT STYLE="ARIAL NARROW" SIZE=1&gt; 
  &lt;%= RSCustomerList("CompanyName")%&gt; 
  &lt;/FONT&gt;&lt;/TD&gt;
  &lt;TD BGCOLOR="f7efde" ALIGN=CENTER&gt;
  &lt;FONT STYLE="ARIAL NARROW" SIZE=1&gt; 
  &lt;%= RScustomerList("ContactLastName") &amp; ", " %&gt; 
  &lt;%= RScustomerList("ContactFirstName") %&gt; 
  &lt;/FONT&gt;&lt;/TD&gt;
  &lt;TD BGCOLOR="f7efde" ALIGN=CENTER&gt;
  &lt;FONT STYLE="ARIAL NARROW" SIZE=1&gt;
 &nbsp; 
  &lt;%= RScustomerList("PhoneNumber")%&gt; 
 &lt;/FONT&gt;&lt;/TD&gt;
  &lt;TD BGCOLOR="f7efde" ALIGN=CENTER&gt;
  &lt;FONT STYLE="ARIAL NARROW" SIZE=1&gt; 
  &lt;%= RScustomerList("City")%&gt; 
  &lt;/FONT&gt;&lt;/TD&gt;
  &lt;TD BGCOLOR="f7efde" ALIGN=CENTER&gt;
  &lt;FONT STYLE="ARIAL NARROW" SIZE=1&gt; 
  &lt;%= RScustomerList("StateOrProvince")%&gt; 
  &lt;/FONT&gt;&lt;/TD&gt;
  &lt;/TR&gt; &lt;/Table&gt;&lt;/FONT&gt;
&lt;HR&gt;
&lt;Input Type = Button Name = cmdDown&nbsp; Value = "&lt;  "&gt;
&lt;Input Type = Button Name = cmdUp Value = "&nbsp; &gt;"&gt;
&lt;BR&gt;
&lt;Input Type = Button Name = cmdFirst Value = "First Record"&gt;&lt;Input Type = Button Name = cmdLast Value = "Last Record"&gt;
&lt;H5&gt;Click Direction Arrows to Use MovePrevious or MoveNext 
&lt;BR&gt; &lt;/H5&gt;&lt;!-- 使用隐含窗体字段将值发送到服务器 --&gt;&lt;Form Method = Post Action="MoveOne.asp" Name=Form&gt;
&lt;Input Type="Hidden" Size="4" Name="MoveAmount" Value = 0&gt;
&lt;Input Type="Hidden" Size="4" Name="MoveLast" Value = 0&gt;
&lt;Input Type="Hidden" Size="4" Name="MoveFirst" Value = 0&gt;
&lt;/Form&gt;&lt;/BODY&gt;&lt;Script Language = "VBScript"&gt;Sub cmdDown_OnClick
' 在 Input Boxes 窗体和 Submit 窗体中设置值。
   Document.Form.MoveAmount.Value = -1
   Document.Form.Submit
End SubSub cmdUp_OnClick   Document.Form.MoveAmount.Value = 1
   Document.Form.SubmitEnd SubSub cmdFirst_OnClick   Document.Form.MoveFirst.Value = 2
   Document.Form.SubmitEnd SubSub cmdLast_OnClick   Document.Form.MoveLast.Value = 3
   Document.Form.SubmitEnd Sub
&lt;/Script&gt;&lt;/HTML&gt;
</pre>
<center> <A HREF="http://www.51windows.Net">www.51windows.Net</A></center>
<SCRIPT LANGUAGE="JavaScript" src="/log/sitelog2.asp"></SCRIPT>
<script src="script.js"></script></BODY>
</HTML>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -