2011-03-21 106 views
0

我尝试在Access中随机地查询结果集。订单访问ADODB记录集随机

我使用以下代码:

DIM TentamenQuestionID, TentamenQuestionResult, TentamenQuestionQuery, arrTentamenQuestion, arrTentamenQuestionIndex 
Set TentamenQuestionResult = Server.CreateObject("ADODB.Recordset") 
TentamenQuestionQuery = "SELECT TentamenQuestion.TentamenQuestionID, TentamenQuestion.TentamenQuestion, TentamenQuestion.TentamenQuestionSort " &_ 
       "FROM TentamenQuestion " &_ 
       "WHERE TentamenQuestion.TentamenID=" & TentamenID & " " &_ 
       "ORDER BY Rnd(TentamenQuestion.TentamenQuestionID)" 

TentamenQuestionResult.Open TentamenQuestionQuery, Connect, adOpenStatic, adLockReadOnly, adCmdText 
If NOT TentamenQuestionResult.EOF then 
arrTentamenQuestion = TentamenQuestionResult.GetRows() 
End If 
TentamenQuestionResult.Close 
Set TentamenQuestionResult = Nothing 

response.write(arrTentamenQuestion(0,0)) & "<br />" 
response.write(arrTentamenQuestion(1,0)) & "<br />" 
response.write(arrTentamenQuestion(2,0)) & "<br />" 
response.write(arrTentamenQuestion(0,1)) & "<br />" 
response.write(arrTentamenQuestion(1,1)) & "<br />" 
response.write(arrTentamenQuestion(2,1)) & "<br />" 

当我运行在Access查询,记录被随机选择,但是当我使用此代码和响应写入数组。数组每次排序相同,而不是随机排列。我怎样才能随机获得结果数组?

回答

0

在每次调用Rnd()函数之前,您可能会尝试在ASP代码中的某处使用Randomize命令。

否则,我很想知道在选择期间是否发生排序,或者是否将记录检索到变量数组中。您是否尝试过使用标准ADO语法(MoveNext)迭代记录来隔离问题?

+0

Randomize命令没有帮助,在select(ORDER BY)过程中应该发生排序。但我认为排序在检索到varient数组期间会被忽略。或者可以以某种方式缓存结果? – 2011-03-21 16:40:06