为了增强性能和资源,我刚开始在我的一些脚本上使用getRows()。我刚遇到一个问题,我想问一下。
我这样做是为了得到记录集并得到计数:
If NOT rs.EOF Then
arrResultSet = rs.GetRows()
arrRowCount = UBound(arrResultSet,2)
End If
但后来我意识到我错过了一条记录,所以我在计数中加了1:
If NOT rs.EOF Then
arrResultSet = rs.GetRows()
arrRowCount = UBound(arrResultSet,2) + 1
End If
但是现在当我尝试访问数据数组时,我的脚本稍后会出现错误,这完全是为了向我的计数添加一个数据:
For iCounter = 0 to arrRowCount
...some code...
If LCase(Trim(peopleWord)) = LCase(Trim(arrResultSet(1,iCounter))) Then
...some code...
Next
Microsoft VBScript runtime error '800a0009'
Subscript out of range: 'lcase(...)'
非常感谢任何帮助。
答案 0 :(得分:6)
您的For
从0的索引到arrRowCount
的索引。
所以,例如,如果你有三条记录,你将从0到3,即4,对吗? IIRC,我们曾经这样做:For iCounter = 0 to arrRowCount - 1
编辑:也许这个例子会对您有所帮助。这个web page详细说明了为什么使用GetRows
会带来性能提升,因此我认为您正走在正确的轨道上。我已经包含了整个代码示例,但您对最后的部分感兴趣。它比您使用的代码更少,变量更少。它看起来更干净,更简单。
' Establish the connection object
strConn = "[connection string goes here]"
set dbConn = Server.CreateObject("ADO.Connection")
dbConn.Open strConn
' Establish the recordset object
set rsCustomers = Server.CreateObject("ADO.Recordset")
set rsCustomers.ActiveConnection = dbConn
' Load the recordset object based on supplied query
strSQL = "SELECT RecID, FirstName, LastName FROM Customers"
rsCustomers.Open strSQL
' Push the results into a two-dimensional array
dataArray = rsCustomers.GetRows()
' Cleanup the objects. We do it here instead of at the end because the data
' has already been placed into an array. This is an advantage in that we can release
' memory sooner.
rsCustomers.Close
set rsCustomers = nothing
dbConn.Close
set dbConn = nothing
' Retrieve the records performing business logic where necessary
jMax = ubound(dataArray, 2)
for j = 0 to jMax
'Additional business logic here
next