码迷,mamicode.com
首页 > Windows程序 > 详细

C#分页

时间:2016-01-03 14:59:27      阅读:220      评论:0      收藏:0      [点我收藏+]

标签:

/// <summary>
           /// </summary>
            /// <param name="field">分页依据的字段(单表一般为主键)</param>
       /// <param name="sort">分页的排序规则</param>
       /// <param name="pageNo">页码</param>
       /// <param name="pageSize">每页条数</param>
      /// <param name="total">总记录数</param>
      /// <returns></returns>
       public DataSet UserTermInfo( string field,  string sort, int pageNo, int pageSize, out int total)
       {
         
           StringBuilder sbd = new StringBuilder(@"select a.*,b.DeptName from dbo.UserInfo a,dbo.Department b
                              where a.deptID=b.deptID ");          

     string strSql = GetPagerSql(sbd.ToString(), field, sort, pageNo, pageSize);
           DataSet ds = SqlHelper.Instance.ExecSqlDataSet(strSql);
           total = Convert.ToInt32(ds.Tables[1].Rows[0][0]);
           return ds;
       }

/// <summary>
       /// 获取分页的sql语句
       /// </summary>
       /// <param name="sql">分页前的查询sql语句</param>
       /// <param name="field">分页依据的字段(单表一般为主键)</param>
       /// <param name="sort">分页的排序规则</param>
       /// <param name="pageNo">页码</param>
       /// <param name="pageSize">每页数据条数</param>
       /// <returns>实现了分页功能的sql语句</returns>
       private static string GetPagerSql(string sql, string field, string sort, int pageNo, int pageSize)
       {
           return string.Format(
               @"select * from (
                 select *,row_number() over (order by [{0}] {1}) __PagerRowNoField from (
                  {2}
                 ) s
                ) t where __PagerRowNoField between {3} and {4};
                select count(*) from ({2}) v",
           field, sort, sql, (pageNo - 1) * pageSize + 1, pageNo * pageSize);
       }

C#分页

标签:

原文地址:http://www.cnblogs.com/yf1019091807/p/5096296.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!