ITKeyword,专注技术干货聚合推荐

注册 | 登录

解决database - Classic ASP - BOF

itPublisher 分享于

2021腾讯云限时秒杀,爆款1核2G云服务器298元/3年!(领取2860元代金券),
地址https://cloud.tencent.com/act/cps/redirect?redirect=1062

2021阿里云最低价产品入口+领取代金券(老用户3折起),
入口地址https://www.aliyun.com/minisite/goods

I am trying to run two WHILE NOT loops for a recordset. One of the loops counts the number of items whilst the other prints the results. I cannot alter the SQL query, so this is the counting method I'm left with.

setPeopleCount = 0
While NOT rsSetContents.EOF
    setPeopleCount = setPeopleCount + 1
rsSetContents.MoveNext
Wend

While NOT rsSetContents.EOF
    Response.Write rs.Fields("exampleItem")&"<br>"
rsSetContents.MoveNext
Wend

My problem is running the two loops. After the first loop has finished the count, the record cursor is at the end of the file, so when the next loop needs to run - it doesn't because EOF is true.

How can I reset the cursor back to the beginning of the file so the second loop can run?

database asp-classic vbscript ado resultset
|
  this question
edited Dec 20 '11 at 22:45 Jonathan Allen 34.1k 51 174 343 asked Dec 20 '11 at 22:41 TheCarver 8,553 18 66 125 1   Why do you need the count first? –  John Saunders Dec 20 '11 at 22:43      Why don't you get the count in a seperate statement like SELECT COUNT ? Or Even use GetRows() and use UBound(GetRowsArray,2) to get the count of records. –  Control Freak Dec 22 '11 at 9:22

 | 

3 Answers
3

解决方法

You can use MoveFirst.

http://msdn.microsoft.com/en-us/library/windows/desktop/ms677527(v=vs.85).aspx


|
  this answer
answered Dec 20 '11 at 22:45 Jonathan Allen 34.1k 51 174 343      Thanks, consider yourself a tick (when 7 minutes is up) –  TheCarver Dec 20 '11 at 22:49

 | 

Could you not count on the bottom loop? Or perhaps read the records into an object array then you are free to iterate over it as many times as u want


|
  this answer
answered Dec 20 '11 at 22:48 RockThunder 146 4      Unfortunately not, I need to use the count before the second loop starts. I've just used rs.MoveFirst and it worked. Thanks anyway –  TheCarver Dec 20 '11 at 22:50

 | 

The MoveFirst requires proper cursor on the recordset - if for example you'll change to different database the default cursor might change and the code might fail.

I would suggest you to store the values while counting, thus save the second loop:

setPeopleCount = 0
Dim exampleItems()
ReDim exampleItems(-1)
While NOT rsSetContents.EOF
    setPeopleCount = setPeopleCount + 1
    ReDim Preserve exampleItems(UBound(exampleItems) + 1)
    exampleItems(UBound(exampleItems)) = rs("exampleItem")
    rsSetContents.MoveNext
Wend

'instead of a loop, just this:
Response.Write(Join(exampleItems, "<br />"))

|
  this answer
answered Dec 21 '11 at 13:05 Shadow Wizard 50.2k 15 92 140

 | 


相关阅读排行


相关内容推荐

最新文章

×

×

请激活账号

为了能正常使用评论、编辑功能及以后陆续为用户提供的其他产品,请激活账号。

您的注册邮箱: 修改

重新发送激活邮件 进入我的邮箱

如果您没有收到激活邮件,请注意检查垃圾箱。