ASP代碼實現access隨機顯示不重複記錄

來源:互聯網
上載者:User

www.veryhuo.com]

<% 
'-------------------------資料庫連接----------------------- 
Set objConn = Server.CreateObject("ADODB.Connection") 
objConn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" &_ 
"Data Source=" & Server.MapPath("data.mdb") 
objConn.Open 
'-------------------------資料庫連接----------------------- 
'-------------------------檢索資料----------------------- 
strSQL = "SELECT id,DataColumn FROM DataTable"   
'Sql語句,檢索資料庫 
Set objRS = Server.CreateObject("ADODB.Recordset")   
'建立記錄集 
objRS.Open strSQL, objConn, 1, 1     
'執行檢索 
Count=objRS.RecordCount       
'得到記錄總數 
Item=4                                     
'顯示記錄數 
'-------------------------檢索資料----------------------- 
'------------------------------------------------------------------------------- 
redim a(Item, 2),t(Count) 
'定義2數組,數組a用來儲存記錄,數組t用來刪選記錄 
'--------------------------------------- 
'初始數組數值,目的為了插入資料以後和此值做比較 
for each j in t 
j=0 
next 
'--------------------------------------- 
'--------------------------------------- 
' 隨機抽取記錄號 
Randomize timer    '初始化隨機數產生器 
for j=1 to Item 
k=int(rnd*Count+1)  '從總數裡面隨機取一條記錄 
do while t(k)<>0            '判斷是否記錄是否已經在數組中 
k=int(rnd*Item+1) 
loop 
t(k)=1       '第k條記錄被選中 
next 
'-------------------------------------- 
j=1:i=1'定義下標 
'-------------------------------------- 
' 迴圈選取資料集objRS中的部分記錄存放到數組中 
Do While Not objRS.Eof 
if t(j)=1 then 
a(i,1)=objRS("id")        '記錄id 
a(i,2)=objRS("DataColumn") '記錄內容 
i=i+1 
end if 
j=j+1 
objRS.MoveNext 
Loop 
'-------------------------------------- 
'------------------------------------------------------------------------------- 
'----------------------------顯示內容-------------------- 
for i=1 to Item 
Response.write "序號"&a(i,1)&"<br>" 
Response.write "內容"&a(i,2)&"<p>" 
next 
'----------------------------顯示內容-------------------- 
'--------------------------- 
'釋放資源 
objRs.Close 
set objRs=nothing 
objConn.Close 
set objConn=nothing 
'--------------------------- 
%>

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.