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
Public
Sub
Main()
On
Error
GoTo
ErrorHandler
' connection and recordset variables
Dim
rstEmployees
As
ADODB.Recordset
Dim
Cnxn
As
ADODB.Connection
Dim
strSQLEmployees
As
String
Dim
strCnxn
As
String
' array variable
Dim
arrEmployees
As
Variant
' detail variables
Dim
strMessage
As
String
Dim
intRows
As
Integer
Dim
intRecord
As
Integer
' open connection
Set
Cnxn =
New
ADODB.Connection
strCnxn =
"Provider='sqloledb';Data Source='MySqlServer';"
& _
"Initial Catalog='Pubs';Integrated Security='SSPI';"
Cnxn.Open strCnxn
' open recordset client-side to enable RecordCount
Set
rstEmployees =
New
ADODB.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
If
intRows <= 0
Then
MsgBox
"Please enter a positive number"
, vbOKOnly,
"Not less than zero!"
' if number of requested records is over the total
ElseIf
intRows > rstEmployees.RecordCount
Then
MsgBox
"Not enough records in Recordset to retrieve "
& intRows &
" rows."
, _
vbOKOnly,
"Over the available total"
Else
Exit
Do
End
If
Loop
' else put the data in an array and print
arrEmployees = rstEmployees.GetRows(intRows)
Dim
x
As
Integer
, y
As
Integer
For
x = 0
To
intRows - 1
For
y = 0
To
2
Debug.Print arrEmployees(y, x) &
" "
;
Next
y
Debug.Print vbCrLf
Next
x
' clean up
rstEmployees.Close
Cnxn.Close
Set
rstEmployees =
Nothing
Set
Cnxn =
Nothing
Exit
Sub
ErrorHandler:
' clean up
If
Not
rstEmployees
Is
Nothing
Then
If
rstEmployees.State = adStateOpen
Then
rstEmployees.Close
End
If
Set
rstEmployees =
Nothing
If
Not
Cnxn
Is
Nothing
Then
If
Cnxn.State = adStateOpen
Then
Cnxn.Close
End
If
Set
Cnxn =
Nothing
If
Err <> 0
Then
MsgBox Err.Source &
"-->"
& Err.Description, ,
"Error"
End
If
End
Sub
'EndGetRowsVB
|