程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 網頁編程 >> ASP編程 >> ASP技巧 >> 實驗Recordset.Movenext,Recordset.Previous,Recorset...等移動記錄

實驗Recordset.Movenext,Recordset.Previous,Recorset...等移動記錄

編輯:ASP技巧

我在實驗Recordset.Movenext,Recordset.PRevious,Recorset.......等移動記錄的時候遇到了困難.
我用Access做後台數據庫,通過一個查詢條件查詢得一個記錄集,我想用一個表格實現記錄的逐條浏覽(不是一組記錄分頁浏覽). 即"共查到**條記錄,現在是第*條",單擊"Next"按鈕後下一條記錄的內容顯示在表格中.....

我用如下代碼實現(見最後)

發現若將Rst.Open "select * from 某表 where 從某個Form獲取的查詢條件"
改成  Rst.Open "select * from 某表"(相當於不做查詢動作)後, 記錄的浏覽功能才得以實現.
否則總是出現" EOF或BOF為真 "的錯誤提示.可我明明可以從"某表"中 "select... where..."到好幾個記錄的!

總之如何將"記錄的逐條浏覽"和"select * from 某表 where 從某個Form獲取的查詢條件" 結合起來?

望予以指點為感!

楊利
2000/4/13

程序代碼:

<%@ LANGUAGE=VBScript %>
<!-- #Include file="ADOVBS.INC" -->
<Html>
<head>
<title></title>
</head>
<body BGCOLOR="#FFFFF0">
<h3 align="center"><font face="隸書" color="#004080"><big>現在您可以編輯以下記錄</big></font></h3>
<!-- 在服務器上創建 Connection 和 Recordset 對象 -->
<%
'創建並打開 Connection 對象。
Set cn=Server.CreateObject("ADODB.Connection")
    cn.Open "DSN=數據庫名"
'創建並打開 Recordset 對象。
Set Rst = Server.CreateObject("ADODB.Recordset")
    Rst.ActiveConnection = cn
    Rst.CursorType = adOpenKeyset
    Rst.LockType = adLockOptimistic  
Rst.Open "select * from 某表 where 性別='"&request.form("t1")&"'"(執行這句大有問題)
Rst.Open "select * from 某表 where 性別='男'"(執行這句有點問題)
Rst.Open "select * from hr_base"(執行這句沒有問題)
' 檢查 Request.Form 集合以查看所記錄的任何移動。
If Not IsEmpty(Request.Form("MoveAmount")) Then
' 跟蹤該會話的移動數目和方向。
   session("Moves") = Session("Moves") + Request.Form("MoveAmount")
   Clicks = Session("Moves")
'移動到上一個已知位置。
   Rst.Move CInt(Clicks)
'檢查移動為 + 還是 - 並進行錯誤檢查。
      If CInt(Request.Form("MoveAmount")) = 1 Then
         If Rst.EOF Then
            Session("Moves") = Rst.RecordCount
            Rst.MoveLast
         End If  
         Rst.MoveNext
      End If
      If Request.Form("MoveAmount") < 1 Then
         Rst.MovePrevious
      End If
'檢查有無單擊 First Record 或 Last Record 命令按鈕。
      If Request.Form("MoveLast") = 3 Then
         Rst.MoveLast
         Session("Moves") = Rst.RecordCount
      End If
      If Request.Form("MoveFirst") = 2 Then
         Rst.MoveFirst
         Session("Moves") = 1
      End If
End If
' 對 Move Button 單擊組合進行錯誤檢查。
      If Rst.EOF Then
         Session("Moves") = Rst.RecordCount
         Rst.MoveLast
         Response.Write "This is the Last Record"
         End If  
         If Rst.BOF Then
         Session("Moves") = 1
         Rst.MoveFirst
         Response.Write "This is the First Record"
      End If
   %>
<!-- 顯示當前記錄數目和記錄集大小-->
<h3 align="center"><font face="隸書" color="#004080">共查到</font><font color="#600060"><%=Rst.RecordCount%></font><font face="隸書" color="#004080">條記錄,當前為第</font>
<font color="#600060">
<%
If IsEmpty(Session("Moves"))  Then
           Session("Moves") =1
End If
%>
<%Response.Write(Session("Moves") )%>
</font><font face="隸書" color="#004080">條記錄</font></h3>
<hr align="center">
<p align="center">  <input Type="button" Name="cmdFirst" Value="第一條"                                    
><input Type="button" Name="cmdDown" Value="上一條"><input                                    
Type="button" Name="cmdUp" Value="下一條"><input Type="button" Name="cmdLast"                                    
Value="末一條"> </p>                                                                    
<p align="center"><b><font size="5" color="#000080" face="隸書">查詢結果:</font></b></p>                                       
                                       
<table>
(用於逐條顯示記錄的表格)                                      
</table>  
                                   
<!-- 使用隱含窗體字段將值發送到服務器-->                                                                      
<form Method="Post" Action Name="Form">                                   
  <input type="hidden" name="MoveAmount" value="0"><input type="hidden" name="MoveLast"                                   
  value="0"><input type="hidden" name="MoveFirst" value="0">                                   
</form>                                   
</body>                                   
<script Language="VBScript">                                                                       
Sub cmdDown_OnClick                                   
'在 Input Boxes 窗體和 Submit 窗體中設置值。                                                                
   Document.Form.MoveAmount.Value = -1                                   
   Document.Form.Submit                                   
End Sub                                                                      
Sub cmdUp_OnClick                                                                       
   Document.Form.MoveAmount.Value = 1                                   
   Document.Form.Submit                                   
End Sub                                                                       
Sub cmdFirst_OnClick                                                                      
   Document.Form.MoveFirst.Value = 2   Document.Form.Submit                                     
End Sub                                                                       
Sub cmdLast_OnClick                                                                       
   Document.Form.MoveLast.Value =3                                   
   Document.Form.Submit                                   
End Sub                                   
</script>                                                           
</Html>

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved