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

📄 mdmthgetrowsx.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>GetRows 方法范例</title>
<style>@import url(msdn_ie4.css);</style>
</HEAD>
<BODY>
<h3><a name="mdmthgetrowsx"></a>GetRows 方法范例</h3>
<p>
该范例使用 <b>GetRows</b> 方法从 <b>Recordset</b> 中检索指定数目的行,并将结果数据填充到数组。在两种情况下 <b>GetRows</b> 方法返回的行将少于所需的数目:一种情况是因为达到了 <b>EOF</b>,另一种情况是因为 <b>GetRows</b> 试图检索已被其他用户删除的数据。仅当第二种情况发生时函数将返回 <b>False</b>。运行该过程需要使用 GetRowsOK 函数。</p>
<pre>Public Sub GetRowsX()   Dim rstEmployees As ADODB.Recordset
   Dim strCnn As String
   Dim strMessage As String
   Dim intRows As Integer
   Dim avarRecords As Variant
   Dim intRecord As Integer   ' 使用雇员表中的姓名和受雇日期打开记录集。
      strCnn = "Provider=sqloledb;" &amp; _
      "Data Source=srv;Initial Catalog=pubs;User Id=sa;Password=; "
   Set rstEmployees = New ADODB.Recordset
   rstEmployees.Open "SELECT fName, lName, hire_date " &amp; _
      "FROM Employee ORDER BY lName", strCnn, , , adCmdText   Do While True
      ' 得到用户输入的行数。
      strMessage = "Enter number of rows to retrieve."
      intRows = Val(InputBox(strMessage))      If intRows &lt;= 0 Then Exit Do      ' 如 GetRowsOK 成功则打印结果,请注意是否达到文件末端。
      If GetRowsOK(rstEmployees, intRows, _
            avarRecords) Then
         If intRows &gt; UBound(avarRecords, 2) + 1 Then
            Debug.Print "(Not enough records in " &amp; _
               "Recordset to retrieve " &amp; intRows &amp; _
               " rows.)"
         End If
         Debug.Print UBound(avarRecords, 2) + 1 &amp; _
            " records found."         ' 打印已检索的数据。
         For intRecord = 0 To UBound(avarRecords, 2)
            Debug.Print "&nbsp; " &amp; _
               avarRecords(0, intRecord) &amp; " " &amp; _
               avarRecords(1, intRecord) &amp; ", " &amp; _
               avarRecords(2, intRecord)
         Next intRecord
      Else
         ' 假定 GetRows 错误源于其他用户对数据的更改,
         ' 使用 Requery 刷新 Recordset 并重新开始。
         If MsgBox("GetRows failed--retry?", _
               vbYesNo) = vbYes Then
            rstEmployees.Requery
         Else
            Debug.Print "GetRows failed!"
            Exit Do
         End If
      End If      ' 由于使用 GetRows 使当前记录指针指向访问过的最后一个记录,
      ' 所以,在循环回到另一次搜索前将记录指针移回 Recordset 的开始。
      rstEmployees.MoveFirst
   Loop   rstEmployees.CloseEnd SubPublic Function GetRowsOK(rstTemp As ADODB.Recordset, _
   intNumber As Integer, avarData As Variant) As Boolean   ' 将 GetRows 方法的结果保存在数组中。
   avarData = rstTemp.<i>GetRows</i>(intNumber)
   ' 仅当返回的行数少于所需的行数而非由于到达了 Recordset 末端时才返回 False。
   If intNumber &gt; UBound(avarData, 2) + 1 And _
         Not rstTemp.EOF Then
      GetRowsOK = False
   Else
      GetRowsOK = True
   End IfEnd Function
</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 + -