這篇文章主要講解了“mssql access數(shù)據(jù)庫(kù)利用top分頁(yè)的方法”,文中的講解內(nèi)容簡(jiǎn)單清晰,易于學(xué)習(xí)與理解,下面請(qǐng)大家跟著小編的思路慢慢深入,一起來(lái)研究和學(xué)習(xí)“mssql access數(shù)據(jù)庫(kù)利用top分頁(yè)的方法”吧!
創(chuàng)新互聯(lián)公司專(zhuān)業(yè)為企業(yè)提供徐水網(wǎng)站建設(shè)、徐水做網(wǎng)站、徐水網(wǎng)站設(shè)計(jì)、徐水網(wǎng)站制作等企業(yè)網(wǎng)站建設(shè)、網(wǎng)頁(yè)設(shè)計(jì)與制作、徐水企業(yè)網(wǎng)站模板建站服務(wù),10余年徐水做網(wǎng)站經(jīng)驗(yàn),不只是建網(wǎng)站,更提供有價(jià)值的思路和整體網(wǎng)絡(luò)服務(wù)。
復(fù)制代碼 代碼如下:
uusing system.collections.generic;sing system;using system.text;////// 構(gòu)造分頁(yè)后的sql語(yǔ)句/// public static class paginghelper{ ////// 獲取分頁(yè)sql語(yǔ)句,排序字段需要構(gòu)成唯一記錄 /// /// 記錄總數(shù) /// 每頁(yè)記錄數(shù) /// 當(dāng)前頁(yè)數(shù) /// sql查詢(xún)語(yǔ)句 /// 排序字段,多個(gè)則用“,”隔開(kāi) ///分頁(yè)sql語(yǔ)句 public static string createpagingsql(int _recordcount, int _pagesize, int _pageindex, string _safesql, string _orderfield) { //重新組合排序字段,防止有錯(cuò)誤 string[] arrstrorders = _orderfield.split(new char[] { ',' }, stringsplitoptions.removeemptyentries); stringbuilder sboriginalorder = new stringbuilder(); //原排序字段 stringbuilder sbreverseo教程rder = new stringbuilder(); //與原排序字段相反,用于分頁(yè) for (int i = 0; i < arrstrorders.length; i++) { arrstrorders[i] = arrstrorders[i].trim(); //去除前后空格 if (i != 0) { sboriginalorder.append(", "); sbreverseorder.append(", "); } sboriginalorder.append(arrstrorders[i]); int index = arrstrorders[i].indexof(" "); //判斷是否有升降標(biāo)識(shí) if (index > 0) { //替換升降標(biāo)識(shí),分頁(yè)所需 bool flag = arrstrorders[i].indexof(" desc", stringcomparison.ordinalignorecase) != -1; sbreverseorder.appendformat("{0} {1}", arrstrorders[i].remove(index), flag ? "asc" : "desc"); } else { sbreverseorder.appendformat("{0} desc", arrstrorders[i]); } } //計(jì)算總頁(yè)數(shù) _pagesize = _pagesize == 0 ? _recordcount : _pagesize; int pagecount = (_recordcount + _pagesize - 1) / _pagesize; //檢查當(dāng)前頁(yè)數(shù) if (_pageindex < 1) { _pageindex = 1; } else if (_pageindex > pagecount) { _pageindex = pagecount; } stringbuilder sbsql = new stringbuilder(); //第一頁(yè)時(shí),直接使用top n,而不進(jìn)行分頁(yè)查詢(xún) if (_pageindex == 1) { sbsql.appendformat(" select top {0} * ", _pagesize); sbsql.appendformat(" from ({0}) as t ", _safesql); sbsql.appendformat(" order by {0} ", sboriginalorder.tostring()); } //最后一頁(yè)時(shí),減少一個(gè)top n else if (_pageindex == pagecount) { sbsql.append(" select * from "); sbsql.append(" ( "); sbsql.appendformat(" select top {0} * ", _recordcount - _pagesize * (_pageindex - 1)); sbsql.appendformat(" from ({0}) as t ", _safesql); sbsql.appendformat(" order by {0} ", sbreverseorder.tostring()); sbsql.append(" ) as t "); sbsql.appendformat(" order by {0} ", sboriginalorder.tostring()); } //前半頁(yè)數(shù)時(shí)的分頁(yè) else if (_pageindex < (pagecount / 2 + pagecount % 2)) { sbsql.append(" select * from "); sbsql.append(" ( "); sbsql.appendformat(" select top {0} * from ", _pagesize); sbsql.append(" ( "); sbsql.appendformat(" select top {0} * ", _pagesize * _pageindex); sbsql.appendformat(" from ({0}) as t ", _safesql); sbsql.appendformat(" order by {0} ", sboriginalorder.tostring()); sbsql.append(" ) as t "); sbsql.appendformat(" order by {0} ", sbreverseorder.tostring()); sbsql.append(" ) as t "); sbsql.appendformat(" order by {0} ", sboriginalorder.tostring()); } //后半頁(yè)數(shù)時(shí)的分頁(yè) else { sbsql.appendformat(" select top {0} * from ", _pagesize); sbsql.append(" ( "); sbsql.appendformat(" select top {0} * ", ((_recordcount % _pagesize) + _pagesize * (pagecount - _pageindex))); sbsql.appendformat(" from ({0}) as t ", _safesql); sbsql.appendformat(" order by {0} ", sbreverseorder.tostring()); sbsql.append(" ) as t "); sbsql.appendformat(" order by {0} ", sboriginalorder.tostring()); } return sbsql.tostring(); } ////// 獲取記錄總數(shù)sql語(yǔ)句 /// /// 限定記錄數(shù) /// sql查詢(xún)語(yǔ)句 ///記錄總數(shù)sql語(yǔ)句 public static string createtopnsql(int _n, string _safesql) { return string.format(" select top {0} * from ({1}) as t ", _n, _safesql); } ////// 獲取記錄總數(shù)sql語(yǔ)句 /// /// sql查詢(xún)語(yǔ)句 ///記錄總數(shù)sql語(yǔ)句 public static string createcountingsql(string _safesql) { return string.format(" select count(1) as recordcount from ({0}) as t ", _safesql); }}
感謝各位的閱讀,以上就是“mssql access數(shù)據(jù)庫(kù)利用top分頁(yè)的方法”的內(nèi)容了,經(jīng)過(guò)本文的學(xué)習(xí)后,相信大家對(duì)mssql access數(shù)據(jù)庫(kù)利用top分頁(yè)的方法這一問(wèn)題有了更深刻的體會(huì),具體使用情況還需要大家實(shí)踐驗(yàn)證。這里是創(chuàng)新互聯(lián),小編將為大家推送更多相關(guān)知識(shí)點(diǎn)的文章,歡迎關(guān)注!