關於分頁類的改進....

原文頁類的代碼如下:

<script language="vbscript" runat="server">

'----------------------------------------
'     數據庫分頁類 for sqlserver.
'     作者: Robin_Zhang ([email protected])
'     請保留此信息,謝謝。
'----------------------------------------
class splitpage

 public curr_page  '//當前頁碼
 public istart           '//分頁開始的記錄位置
 public iend             '//分頁結束的記錄位置
 public countpage        '//頁數
 public conn             '//數據庫連接對像
 public perpagecount     '//每頁顯示的記錄個數
 public countrecord      '//總記錄數  
 
 public tablename        '//表或視圖名稱
 public key              '//主鍵
 public condition         '//查詢條件
 public orderby          '//排序條件
 public sql_count        '//求總記錄數的sql語句
 public sql_search       '//查詢sql語句
 
 public split_record     '//得到的分頁記錄集
 public url              '//當前頁面的url
 public bar1             '//翻頁條
 public bar2             '//翻頁條
 
 public solitude_sql  '傳送單獨的sql語句
 
 
 '//-------------------private 內部方法集合-----------------------
 
 '得到查詢的記錄數,總頁數,sql語句,得到當前頁數
 private sub getCountRecord()
  if isnull(solitude_sql) or isempty(solitude_sql) then
   sql = "select count("& key &") as totalvalue from "& tablename & condition
  else
   sql = "select count(*) as totalvalue from ("&solitude_sql&") temptablea"
  end if

'  response.write "<hr>"&sql
'  response.end
  
  set rs = conn.execute(sql)
 
  
  countrecord = rs("totalvalue")
  rs.close
  set rs=nothing
  sql_count = sql
  countpage = abs(int(-(countrecord/perpagecount)))
  if curr_page < 1 then curr_page = 1
  if curr_page >countpage then curr_page = countpage
  
  
  
 end sub
 
 '根據當前頁碼和每頁顯示的個數算出起始位置
 private sub count_istart()
  istart = clng((curr_page-1) * perpagecount)
  if istart > clng(countrecord) then istart = clng(countrecord)
  if istart < 0 then istart = 0
 end sub

 
 '根據當前頁碼和每頁顯示的個數算出結束位置
 private sub count_iend()
  iend = clng(istart + perpagecount)
  if iend > clng(countrecord) then iend = clng(countrecord)
 end sub
 
 private function get_record()
  if isnull(solitude_sql) or isempty(solitude_sql) then
   sql = "select * from "& tablename &" where "& key &" not in (select top "& istart &" "& key &" from "& tablename & condition & orderby &") and "& key &" in (select top "& iend &" "& key &" from "& tablename & condition & orderby & ")" & orderby
  else
   sql = "select * from ("& solitude_sql &")a where "& key &" not in (select top "& istart &" "& key &" from ("& solitude_sql &")b"& condition & orderby &") and "& key &" in (select top "& iend &" "& key &" from ("& solitude_sql &")c"& condition & orderby & ")" & orderby
  end if
  'response.write sql

  
  
  sql_search = sql
  set get_record = conn.execute(sql)
  
 end function
 
 private sub get_url()
  url = Request.ServerVariables("url") & "?" & Request.ServerVariables("query_string")
  pos = InStrRev(url,"&page")-1
  if pos > 0 then
   url = mid(url,1,pos)
  
  end if
  
  if Request.ServerVariables("query_string") = "" then
   url = Request.ServerVariables("url") & "?1=1"
  end if
 end sub
 
 private function get_bar_gb2312()
 
  
  bar1 = "目前爲第 "& curr_page &"頁 共計 "& countpage &" 頁 共有 "& countrecord &" 筆資料"
  a1 = "<a href='"& url &"&page=1'>首頁</a> "
  a2 = "<a href='"& url &"&page="& curr_page-1 &"'>上頁</a> "
  a3 = "<a href='"& url &"&page="& curr_page+1 &"'>下頁</a> "
  a4 = "<a href='"& url &"&page="& countpage &"'>末頁</a> "
  curr_page = clng(curr_page) 
 
  if curr_page <=1 then
   a1 = "首頁 "
   a2 = "上頁 "
  end if
  
  if curr_page >= clng(countpage) then
   a3 = "下頁 "
   a4 = "末頁 "
  end if
  
  bar2 = a1 & a2 & a3 & a4
  
  
  

  
 end function
 
 private function get_bar_big5()
 end function
  
 
 
 
 '//---------------------public 供外部調用--------------------------
 
 '存取一條sql
 public sub set_solitude_sql(sql)
  solitude_sql = sql
 end sub
 '取得數據庫連接的方法
 public sub setConn(o)
  set conn = o
 end sub
 
 '設定每頁顯示記錄的個數
 public sub setPerpagecount(o)
  perpagecount = o
 end sub
 
 '設定查詢的表名或視圖名稱
 public sub setTablename(atablename)
  tablename = atablename
 end sub
 
 '設定主鍵
 public sub setKey(akey)
  key = akey
 end sub
 
 '設定查詢條件
 public sub setCondition(acondition)
  condition = " " & acondition
 
 end sub
 
 '設定排序依據
 public sub setOrderby(aorderby)
  orderby = " " & aorderby
 end sub
 
 

 '設定當前頁碼
 public sub setCurr_page(o)
  if o <> "" then
   curr_page = clng(o)
  else
   curr_page = 1
  end if
 end sub
 
 public sub execute()
  call getCountRecord()
  call count_istart()
  call count_iend()
  call get_url()
  set split_record = get_record()
  call get_bar_gb2312()
 end sub
 
 

 

end class
</script>






以前的語句中有一個not in和一個in
select * from table_name where id not in (select top 100 id from table_name order by id desc) and id in (select top 200 id from table_name order by id desc) order by id desc


考驗到in會遍歷整張表.所以做以下修改...

select top 10 * from table_name where id>(select max (id) from (select top ((頁碼-1)*10) id from table_name order by id) as table_temp) order by id

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章