最好多次重新调整数组或重新打开记录集?
Better to ReDim array multiple times or reopen recordset?
我正在用 VBscript 编写代码,打开一个记录集对象,然后加载一个数组,其中的对象包含来自每条记录的数据。我的记录集类型不支持 rs.RecordCount
属性,所以我要么在循环记录集时需要 ReDim Preserve
数组,要么在执行计数循环后重新打开记录集,因为在计数循环似乎不起作用后使用 rs.MoveFirst
...哪个更快?记录集对象中最多只有 7 条记录,所以我最多需要多次 ReDim。
这是我尝试过的一种方法,但 rs.MoveFirst 似乎无法正常工作,请参阅评论:
Function LoadData(filter_val)
Dim arr
Dim rs
'Calls function that opens the rs and returns it
Set rs = GetRS(filter_val)
Dim counter
counter = 0
Do Until rs.EOF
counter = counter + 1
rs.MoveNext
Loop
ReDim arr(counter)
rs.MoveFirst
For i = 0 To counter
Set arr(i) = New obj
'attempt to load values into the object from the recordset, but get an
'error saying 'either BOF or EOF is true, or the current record has been deleted'
'I tried adding If statements with MsgBox print outs checking for rs.EOF or rs.BOF
'being true right after rs.MoveFirst, but neither evaluates to true...
Next
End Function
这个方法有效,但我必须不断地重新调暗数组:
Function LoadData(filter_val)
Dim arr
Dim rs
Set rs = GetRS(filter_val)
Dim counter
counter = 0
ReDim arr(counter)
Do Until rs.EOF
Set arr(counter) = New obj
'load data from rs into object
rs.MoveNext
If Not rs.EOF
counter = counter + 1
ReDim Preserve arr(counter)
End If
Loop
End Function
重新确定数组的尺寸表现出奇的好,所以我会选择 ReDim
。增加数组大小的通用方法是将其初始化为空数组:
ReDim arr(-1)
然后每次迭代都将上边界加一,然后再添加一些东西:
Do Until rs.EOF
ReDim Preserve arr(UBound(arr)+1)
Set arr(UBound(arr)) = New obj
'load data from rs into object
rs.MoveNext
Loop
这样您就不需要计数器变量来跟踪数组大小。
这看起来更简单,来自 MSDN
GetRows Method (ADO)
Retrieves multiple records of a Recordset object into an array.
Syntax
array = recordset.GetRows(Rows, Start, Fields )
Return Value
Returns a Variant whose value is a two-dimensional array.
Parameters
Rows
Optional. A GetRowsOptionEnum value that indicates the number of records to retrieve. The default is adGetRowsRest.
Start
Optional. A String value or Variant that evaluates to the bookmark for the record from which the GetRows operation should begin. You can also use a BookmarkEnum value.
Fields
Optional. A Variant that represents a single field name or ordinal position, or an array of field names or ordinal position numbers. ADO returns only the data in these fields.
如果最大值为 7,也只需将数组调暗为 7。
我正在用 VBscript 编写代码,打开一个记录集对象,然后加载一个数组,其中的对象包含来自每条记录的数据。我的记录集类型不支持 rs.RecordCount
属性,所以我要么在循环记录集时需要 ReDim Preserve
数组,要么在执行计数循环后重新打开记录集,因为在计数循环似乎不起作用后使用 rs.MoveFirst
...哪个更快?记录集对象中最多只有 7 条记录,所以我最多需要多次 ReDim。
这是我尝试过的一种方法,但 rs.MoveFirst 似乎无法正常工作,请参阅评论:
Function LoadData(filter_val)
Dim arr
Dim rs
'Calls function that opens the rs and returns it
Set rs = GetRS(filter_val)
Dim counter
counter = 0
Do Until rs.EOF
counter = counter + 1
rs.MoveNext
Loop
ReDim arr(counter)
rs.MoveFirst
For i = 0 To counter
Set arr(i) = New obj
'attempt to load values into the object from the recordset, but get an
'error saying 'either BOF or EOF is true, or the current record has been deleted'
'I tried adding If statements with MsgBox print outs checking for rs.EOF or rs.BOF
'being true right after rs.MoveFirst, but neither evaluates to true...
Next
End Function
这个方法有效,但我必须不断地重新调暗数组:
Function LoadData(filter_val)
Dim arr
Dim rs
Set rs = GetRS(filter_val)
Dim counter
counter = 0
ReDim arr(counter)
Do Until rs.EOF
Set arr(counter) = New obj
'load data from rs into object
rs.MoveNext
If Not rs.EOF
counter = counter + 1
ReDim Preserve arr(counter)
End If
Loop
End Function
重新确定数组的尺寸表现出奇的好,所以我会选择 ReDim
。增加数组大小的通用方法是将其初始化为空数组:
ReDim arr(-1)
然后每次迭代都将上边界加一,然后再添加一些东西:
Do Until rs.EOF
ReDim Preserve arr(UBound(arr)+1)
Set arr(UBound(arr)) = New obj
'load data from rs into object
rs.MoveNext
Loop
这样您就不需要计数器变量来跟踪数组大小。
这看起来更简单,来自 MSDN
GetRows Method (ADO)
Retrieves multiple records of a Recordset object into an array.
Syntax
array = recordset.GetRows(Rows, Start, Fields )
Return Value
Returns a Variant whose value is a two-dimensional array.
Parameters
Rows Optional. A GetRowsOptionEnum value that indicates the number of records to retrieve. The default is adGetRowsRest. Start Optional. A String value or Variant that evaluates to the bookmark for the record from which the GetRows operation should begin. You can also use a BookmarkEnum value. Fields Optional. A Variant that represents a single field name or ordinal position, or an array of field names or ordinal position numbers. ADO returns only the data in these fields.
如果最大值为 7,也只需将数组调暗为 7。