分頁查詢的乙個幫助類

2021-06-16 01:07:27 字數 1248 閱讀 4193

分頁sql查詢在的應用很多,主要有儲存過程分頁和sql分頁兩種,我比較喜歡用sql分頁,主要是很方便。為了提高查詢效率,應在排序欄位上加索引。sql分頁查詢的原理很簡單,比如你要查100條資料中的30-40條,你先查詢出前40條,再把這30條倒序,再查出這倒序後的前十條,最後把這十條倒序就是你想要的結果。

下面把sql分頁查詢的原理用sql語句表現一下:

--分頁公升序(搜出的結果再倒序)

selecttop10*from(selecttop40*froma01orderbya00asc)astorderbya00desc

--分頁公升序

select*from(selecttop10*from(selecttop40*froma01orderbya00asc)astorderbya00desc)ast1orderbya00asc

--分頁降序(搜出的結果再倒序)

selecttop10*from(selecttop40*froma01orderbya00desc)astorderbya00asc

--分頁降序

select*from(selecttop10*from(selecttop40*froma01orderbya00desc)astorderbya00asc)ast1orderbya00desc

為了應用的方便我把生成sql分頁查詢語句的寫成了類splithelp

應用如下:

/**///分頁查詢例子

//////當前頁

///每頁大小

///資料總條數

///連線

///查詢idbcommand

publicidbcommandsearch(intcurrentpage,intpagesize,outintcount,idbconnectioncn)

{//得到idbcommand

idbcommandcmd=cn.createcommand();

cmd.commandtype=commandtype.text;

cmd.commandtext="selectcount(tabletestid)fromtabletest";

if(cn.state!=connectionstate.open)

cn.open();

//得到資料總數

count=(int)cmd.executescalar();

//搜尋的前n條

inttopall=splitpage.gettopnum(currentpage,pagesize,count);1

乙個分頁類

class page 獲得頁面uri,page為當前頁面傳遞的頁面值,var 為傳遞頁面的引數字串 private function get uri var page if else else if else return url 獲得頁面總數以及前一頁 後一頁 最後一頁 private funct...

分享乙個Redis幫助類

在專案中用到了redis的hash集合,但是servicestack封裝的使用起來不方便,於是就自己封裝了乙個dll,利用的servicestack的pool來動態建立iredisclient例項,建立了乙個抽象類redisoperatorbase封裝了一些基本方法。比較簡單,相信大家一看 就會。下...

乙個通用分頁類

1 功能 這個通用分頁類實現的功能是輸入頁數 第幾頁 和每頁的數目,就能獲得相應的資料。2 實現原理 分頁的實現通常分為兩種,一種是先把資料全查詢出來再分頁,一種是需要多少查詢多少,這裡使用第二種,所以就需要先實現在dao層能夠查詢一定範圍內的資料,這裡就實現通過id作為鍵值,查詢一定範圍內的資料的...