但是有利也有弊,它要求排序字段必须构成唯一记录,且select后的字段列表中,不允许出现与排序字段同名的字段。
虽然sql2k5及以上版本已经提供了rownum()来进行分页处理,但是使用sql2k进行开发的还是较多的
复制代码 代码如下:
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);
}
}
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
更新日志
- 黄乙玲1988-无稳定的爱心肝乱糟糟[日本东芝1M版][WAV+CUE]
- 群星《我们的歌第六季 第3期》[320K/MP3][70.68MB]
- 群星《我们的歌第六季 第3期》[FLAC/分轨][369.48MB]
- 群星《燃!沙排少女 影视原声带》[320K/MP3][175.61MB]
- 乱斗海盗瞎6胜卡组推荐一览 深暗领域乱斗海盗瞎卡组分享
- 炉石传说乱斗6胜卡组分享一览 深暗领域乱斗6胜卡组代码推荐
- 炉石传说乱斗本周卡组合集 乱斗模式卡组最新推荐
- 佟妍.2015-七窍玲珑心【万马旦】【WAV+CUE】
- 叶振棠陈晓慧.1986-龙的心·俘虏你(2006复黑限量版)【永恒】【WAV+CUE】
- 陈慧琳.1998-爱我不爱(国)【福茂】【WAV+CUE】
- 咪咕快游豪礼放送,百元京东卡、海量欢乐豆就在咪咕咪粉节!
- 双11百吋大屏焕新“热”,海信AI画质电视成最大赢家
- 海信电视E8N Ultra:真正的百吋,不止是大!
- 曾庆瑜1990-曾庆瑜历年精选[派森][WAV+CUE]
- 叶玉卿1999-深情之选[飞图][WAV+CUE]