您好,登錄后才能下訂單哦!
這篇文章主要講解了“mssql access數據庫利用top分頁的方法”,文中的講解內容簡單清晰,易于學習與理解,下面請大家跟著小編的思路慢慢深入,一起來研究和學習“mssql access數據庫利用top分頁的方法”吧!
復制代碼 代碼如下:
uusing system.collections.generic;sing system;using system.text;/// <summary>/// 構造分頁后的sql語句/// </summary>public static class paginghelper{ /// <summary> /// 獲取分頁sql語句,排序字段需要構成唯一記錄 /// </summary> /// <param name="_recordcount">記錄總數</param> /// <param name="_pagesize">每頁記錄數</param> /// <param name="_pageindex">當前頁數</param> /// <param name="_safesql">sql查詢語句</param> /// <param name="_orderfield">排序字段,多個則用“,”隔開</param> /// <returns>分頁sql語句</returns> public static string createpagingsql(int _recordcount, int _pagesize, int _pageindex, string _safesql, string _orderfield) { //重新組合排序字段,防止有錯誤 string[] arrstrorders = _orderfield.split(new char[] { ',' }, stringsplitoptions.removeemptyentries); stringbuilder sboriginalorder = new stringbuilder(); //原排序字段 stringbuilder sbreverseo教程rder = new stringbuilder(); //與原排序字段相反,用于分頁 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(" "); //判斷是否有升降標識 if (index > 0) { //替換升降標識,分頁所需 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]); } } //計算總頁數 _pagesize = _pagesize == 0 ? _recordcount : _pagesize; int pagecount = (_recordcount + _pagesize - 1) / _pagesize; //檢查當前頁數 if (_pageindex < 1) { _pageindex = 1; } else if (_pageindex > pagecount) { _pageindex = pagecount; } stringbuilder sbsql = new stringbuilder(); //第一頁時,直接使用top n,而不進行分頁查詢 if (_pageindex == 1) { sbsql.appendformat(" select top {0} * ", _pagesize); sbsql.appendformat(" from ({0}) as t ", _safesql); sbsql.appendformat(" order by {0} ", sboriginalorder.tostring()); } //最后一頁時,減少一個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()); } //前半頁數時的分頁 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()); } //后半頁數時的分頁 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(); } /// <summary> /// 獲取記錄總數sql語句 /// </summary> /// <param name="_n">限定記錄數</param> /// <param name="_safesql">sql查詢語句</param> /// <returns>記錄總數sql語句</returns> public static string createtopnsql(int _n, string _safesql) { return string.format(" select top {0} * from ({1}) as t ", _n, _safesql); } /// <summary> /// 獲取記錄總數sql語句 /// </summary> /// <param name="_safesql">sql查詢語句</param> /// <returns>記錄總數sql語句</returns> public static string createcountingsql(string _safesql) { return string.format(" select count(1) as recordcount from ({0}) as t ", _safesql); }}
感謝各位的閱讀,以上就是“mssql access數據庫利用top分頁的方法”的內容了,經過本文的學習后,相信大家對mssql access數據庫利用top分頁的方法這一問題有了更深刻的體會,具體使用情況還需要大家實踐驗證。這里是億速云,小編將為大家推送更多相關知識點的文章,歡迎關注!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。