ASP/GetRows &数数 [英] ASP / GetRows & Count

查看:14
本文介绍了ASP/GetRows &数数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为了提高性能和资源,我刚刚开始在我的一些脚本中使用 getRows().我刚刚遇到一个问题,想请教一下.

To enhance performance and resources, I've just started to use getRows() on a few of my scripts. I have just come across an issue, which I'd like to ask about.

我这样做是为了获取记录集并获取计数:

I was doing this to get the recordset and to get the count:

If NOT rs.EOF Then
    arrResultSet = rs.GetRows()
    arrRowCount = UBound(arrResultSet,2)
End If

但后来我意识到我错过了一条记录,所以我在计数中加了 1:

But then I realised I was missing a record so I added 1 to my count:

If NOT rs.EOF Then
        arrResultSet = rs.GetRows()
        arrRowCount = UBound(arrResultSet,2) + 1
End If

但现在当我尝试访问数据数组时,我的脚本稍后会出现错误,这纯粹是为了给我的计数加一:

But now I get an error later in my script when I try accessing the data array which is purely down to adding one to my count:

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(...)'

非常感谢任何帮助.

推荐答案

您的 For 正在从索引 0 变为 arrRowCount 的索引.

Your For is going from the index of 0 to the index of the arrRowCount.

因此,例如,如果您有 3 条记录,您会从 0 变为 3,也就是 4,对吗?IIRC,我们曾经这样做:For iCounter = 0 to arrRowCount - 1

So, for example, if you have three records, you are going from 0 to 3, which is 4, right? IIRC, we used to do this: For iCounter = 0 to arrRowCount - 1

编辑:也许这个例子会对你有所帮助.此网页详细说明了为什么使用 GetRows 产生了性能改进,所以我认为你在正确的轨道上.我已经包含了整个代码示例,但是您对最后的部分感兴趣.它的代码和变量比您使用的要少.它看起来更干净,更简单.

Edit: Perhaps this example will help you. This web page details why using GetRows yields a performance improvement, so I think you're on the right track. I have included the entire code sample, but you are interested in the part at the end. It has less code, and fewer variables, than you are using. It looks cleaner, simpler.

' 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

这篇关于ASP/GetRows &数数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆