GetRows 方法范例 (VB)

来源:互联网 发布:数据对比ppt模板下载 编辑:程序博客网 时间:2024/06/06 06:33

GetRows 方法范例 (VB)

GetRows 方法范例 (VB)

本范例使用 GetRows 方法从 Recordset 中检索指定数目的行,并用结果数据填充一个数组。在两种情况下,GetRows 方法返回的行将少于所需的数目:一种情况是到达了 EOF,另一种情况是 GetRows 试图检索被其他用户删除的记录。仅在发生第二种情况时,该函数才返回 False。运行此过程需要 GetRowsOK 函数。

GetRows 方法
将 Recordset 对象的多个记录检索到数组中。

语法
array = recordset.GetRows( Rows, Start, Fields )
返回值
返回 Variant,其值为二维数组。

参数
Rows
可选。GetRowsOptionEnum 值,指示要检索的记录数。默认值为 adGetRowsRest。
Start
可选。String 值或 Variant,计算 GetRows ***作开始处的记录的书签。还可以使用 BookmarkEnum 值。
Fields
可选。Variant,表示单个字段名或序号位置,或者字段名数组或序号位置编号。ADO 仅返回这些字段中的数据。
说明
使用 GetRows 方法将 Recordset 中的记录复制到二维数组中。第一个下标标识字段,第二个下标标识记录编号。当 GetRows 方法返回数据时,array 变量将自动调整到正确大小。

如果未指定 Rows 参数的值,GetRows 方法将自动检索 Recordset 对象中的所有记录。如果请求的记录多于可用的记录,GetRows 仅返回可用的记录数目。

如果 Recordset 对象支持书签,可以通过在 Start 参数中传递该记录的 Bookmark 属性的值来指定 GetRows 方法从哪一个记录开始检索数据。

如果要限制 GetRows 调用返回的字段,可以在 Fields 参数中传递单个字段名/编号,或者字段名/编号的数组。

在调用 GetRows 后,下一个未读取的记录成为当前记录。如果没有其他记录,EOF 属性将被设置为 True。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
'BeginGetRowsVB
 
    'To integrate this code
    'replace the data source and initial catalog values
    'in the connection string
     
PublicSub Main()
    OnError GoTo ErrorHandler
 
     ' connection and recordset variables
    DimrstEmployees AsADODB.Recordset
    DimCnxn AsADODB.Connection
    DimstrSQLEmployees AsString
    DimstrCnxn AsString
    ' array variable
    DimarrEmployees AsVariant
    ' detail variables
    DimstrMessage AsString
    DimintRows AsInteger
    DimintRecord AsInteger
     
    ' open connection
    SetCnxn = NewADODB.Connection
    strCnxn = "Provider='sqloledb';Data Source='MySqlServer';" & _
        "Initial Catalog='Pubs';Integrated Security='SSPI';"
    Cnxn.Open strCnxn
     
    ' open recordset client-side to enable RecordCount
    SetrstEmployees = NewADODB.Recordset
    strSQLEmployees = "SELECT fName, lName, hire_date FROM Employee ORDER BY lName"
    rstEmployees.Open strSQLEmployees, Cnxn, adOpenStatic, adLockReadOnly, adCmdText
     
    ' get user input for number of rows
    Do
        strMessage = "Enter number of rows to retrieve:"
        intRows = Val(InputBox(strMessage))
         
          ' if bad user input exit the loop
        IfintRows <= 0 Then
            MsgBox"Please enter a positive number", vbOKOnly, "Not less than zero!"
        ' if number of requested records is over the total
        ElseIfintRows > rstEmployees.RecordCount Then
            MsgBox"Not enough records in Recordset to retrieve " & intRows & " rows.", _
            vbOKOnly,"Over the available total"
        Else
            ExitDo
        EndIf
    Loop
     
      ' else put the data in an array and print
    arrEmployees = rstEmployees.GetRows(intRows)
     
    Dimx AsInteger, y AsInteger
     
    Forx = 0 TointRows - 1
        Fory = 0 To2
            Debug.Print arrEmployees(y, x) & " ";
        Nexty
        Debug.Print vbCrLf
    Nextx
     
    ' clean up
    rstEmployees.Close
    Cnxn.Close
    SetrstEmployees = Nothing
    SetCnxn = Nothing
    ExitSub
     
ErrorHandler:
    ' clean up
    IfNot rstEmployees IsNothing Then
        IfrstEmployees.State = adStateOpen ThenrstEmployees.Close
    EndIf
    SetrstEmployees = Nothing
     
    IfNot Cnxn IsNothing Then
        IfCnxn.State = adStateOpen ThenCnxn.Close
    EndIf
    SetCnxn = Nothing
     
    IfErr <> 0 Then
        MsgBox Err.Source & "-->"& Err.Description, , "Error"
    EndIf
EndSub
'EndGetRowsVB

http://517sou.net/archives/getrows-%e6%96%b9%e6%b3%95%e8%8c%83%e4%be%8b-vb/

0 0
原创粉丝点击