解决database - Classic ASP - BOF
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
You can use MoveFirst.
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
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
- 1ASP错误类型：Microsoft JET Database Engine (0x80040E09) 不能更新。数据库或对象为只读。
- 2[asp连接access错误]错误类型：Microsoft JET Database Engine (0x80004005) 未指定的错误
- 3asp连接Access出现Microsoft JET Database Engine (0x80004005) 未指定的错误
- 4ASP解决方案-Microsoft JET Database Engine(0x80004005)未指定错误
- 5asp + Access 常见的数据库访问失败问题 Microsoft JET Database Engine 错误 '80004005' 解决办法
- 1rpmdb: Thread died in Berkeley DB library
- 2SAP Netweaver和Hybris的数据库层
- 3【Oracle Database 12c的新特性】
- 4linux下mysql命令(授权用户 基本操作)
- 5Oracle:How to create physical standby database with 11g RMAN DUPLICATE FROM ACTIVE DATABASE [ID 747250.1]
- 6【转载】Using the Entity Framework Repository and UnitOfWork Pattern in C# ASP .NET