美文网首页
MSSQL2012以后的OFFSET分页

MSSQL2012以后的OFFSET分页

作者: niunan | 来源:发表于2017-02-26 11:43 被阅读64次
      /// <summary>
    
        ///分页,使用offset,mssql2012以后有用
        /// </summary> 
        /// <param name="orderstr">如:yydate desc,yytime asc,id desc,必须形成唯一性</param>
        /// <param name="PageSize"></param>
        /// <param name="PageIndex"></param>
        /// <param name="strWhere"></param>
        /// <returns></returns>
        public List<Model.Blog> GetList(  string orderstr, int PageSize, int PageIndex, string strWhere)
        {
            if (!string.IsNullOrEmpty(strWhere))
            {
                strWhere = " where " + strWhere;
            }
            string sql = string.Format(
                    "select * from [blog] {0} order by {1} offset {2} rows fetch next {3} rows only",  
                    strWhere, 
                    orderstr, 
                    (PageIndex - 1) * PageSize,
                    PageSize
                );
            List<Model.Blog> list = new List<Model.Blog>();
            using (var connection = ConnectionFactory.GetOpenConnection())
            {
                list = connection.Query<Model.Blog>(sql).ToList();
            }
            return list;
        }

    相关文章

      网友评论

          本文标题:MSSQL2012以后的OFFSET分页

          本文链接:https://www.haomeiwen.com/subject/qjyowttx.html