2011-12-20 162 views
2

我想爲記錄集運行兩個WHILE NOT循環。其中一個循環計算項目的數量,另一個則打印結果。我無法改變SQL查詢,所以這是我留下的計數方法。經典的ASP - BOF

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 

我的問題是運行兩個循環。第一個循環完成計數後,記錄遊標位於文件的末尾,因此當下一個循環需要運行時 - 它不會因爲EOF爲真。

如何將光標重置迴文件的開頭,以便第二個循環可以運行?

+1

爲什麼你首先需要計數? – 2011-12-20 22:43:09

+0

爲什麼不像SELECT COUNT那樣在單獨的語句中獲得計數?或者甚至使用GetRows()並使用UBound(GetRowsArray,2)來獲取記錄數。 – 2011-12-22 09:22:09

回答

1

你能不能在底部循環計數?或者可能將記錄讀入對象數組,然後您可以隨意多次遍歷它,只要您想要

+0

不幸的是,我需要在第二次循環開始之前使用計數。我剛剛使用rs.MoveFirst,它的工作。不管怎麼說,還是要謝謝你 – TheCarver 2011-12-20 22:50:58

0

MoveFirst需要在記錄集上有適當的遊標 - 例如,如果您要更改爲不同的數據庫,則默認遊標可能會更改和代碼可能會失敗。

我建議你存儲的值,同時計數,從而節省了第二循環:

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 />"))