博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
NET项目中分页方法
阅读量:5137 次
发布时间:2019-06-13

本文共 5678 字,大约阅读时间需要 18 分钟。

///         /// 获得查询分页数据        ///         public DataSet GetList(int pageSize, int pageIndex, string strWhere, string filedOrder, out int recordCount)        {            StringBuilder strSql = new StringBuilder();            strSql.Append("select * FROM view_get_car_hl_faultcode");            if (strWhere.Trim() != "")            {                strSql.Append(" where " + strWhere);            }            recordCount = Convert.ToInt32(DbHelperSQL.GetSingle(PagingHelper.CreateCountingSql(strSql.ToString())));            return DbHelperSQL.Query(PagingHelper.CreatePagingSql(recordCount, pageSize, pageIndex, strSql.ToString(), filedOrder));        }
DbHelperSQL.GetSingle和DbHelperSQL.Query是一个访问数据库的公共类库,第一个为获取总数,第二个为根据sql语句获取DataSet
PagingHelper类中的内容如下:
///       /// 双TOP二分法生成分页SQL类(支持MSSQL、ACCESS)    ///       public static class PagingHelper    {        ///         /// 获取分页SQL语句,排序字段需要构成唯一记录        ///         /// 记录总数        /// 每页记录数        /// 当前页数        /// SQL查询语句        /// 排序字段,多个则用“,”隔开        /// 
分页SQL语句
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 sbReverseOrder = 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 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) + 1) { 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) + 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()); } return sbSql.ToString(); } /// /// 获取记录总数SQL语句 /// /// 限定记录数 /// SQL查询语句 ///
记录总数SQL语句
public static string CreateTopnSql(int _n, string _safeSql) { return string.Format(" SELECT TOP {0} * FROM ({1}) AS T ", _n, _safeSql); } /// /// 获取记录总数SQL语句 /// /// SQL查询语句 ///
记录总数SQL语句
public static string CreateCountingSql(string _safeSql) { return string.Format(" SELECT COUNT(1) AS RecordCount FROM ({0}) AS T ", _safeSql); } }

 

 

转载于:https://www.cnblogs.com/New-world/p/3166619.html

你可能感兴趣的文章
【转】Linux之printf命令
查看>>
关于PHP会话:session和cookie
查看>>
display:none和visiblity:hidden区别
查看>>
C#double转化成字符串 保留小数位数, 不以科学计数法的形式出现。
查看>>
利用IP地址查询接口来查询IP归属地
查看>>
构造者模式
查看>>
http和https的区别
查看>>
Hbuild在线云ios打包失败,提示BuildConfigure Failed 31013 App Store 图标 未找到 解决方法...
查看>>
找到树中指定id的所有父节点
查看>>
4----COM:a Generative Model for group recommendation(组推荐的一种生成模型)
查看>>
UVA 11137 - Ingenuous Cubrency
查看>>
js阻止事件冒泡的两种方法
查看>>
Java异常抛出
查看>>
74HC164应用
查看>>
变量声明和定义的关系
查看>>
Wpf 之Canvas介绍
查看>>
linux history
查看>>
jQuery on(),live(),trigger()
查看>>
Python2.7 urlparse
查看>>
sencha touch在华为emotion ui 2.0自带浏览器中圆角溢出的bug
查看>>