Sql通用分页存储过程

这个是一个基于FrameWork平台,C#语言与sql创建分页存储过程

1.使用DbHelperSQL帮助类
2,Ado连接数据库
3,通用存储过程


CREATE proc [dbo].[p_paging]
@tableName varchar(8000),          --表名、视图名
@indexCol varchar(50) = 'id',      --标识列名(如:比如主键、标识,推荐使用索引列)
@pageSize int = 10,                --页面显示条数
@pageIndex int = 0,                --当前页
@orderCol varchar(100) = 'id desc',--排序 (如:id)
@where varchar(max) = '',          --条件
@columns varchar(500) = '*'        --要显示的列
as
declare @sql varchar(max)          --定义字符串
declare @sql2 varchar(max)
declare @where2 varchar(max)	   --定义按条件查询的字符串

if @where <> ''    --如果条件不为空
begin 
    select @where2 = ' And ' + @where   
    select @where = ' Where ' + @where
end
else
    select @where2 = ''   --如果条件为空 执行where2中传递过来的参数


select @sql = 'Select Top ' + Convert(varchar(10),@pageSize) + ' ' + @columns + ' From ' + @tableName           --查询前几条,切查询的列表及查询的表
select @sql2 = @sql + @where			   --添加条件
select @sql =  @sql + ' Where ' + '(' + @indexCol + ' Not In (Select Top ' + Convert(varchar(10), @pageSize * @pageIndex) + ' ' + @indexCol + ' From ' + @tableName + @where +  ' Order by '+ @orderCol +'))'
select @sql = @sql + @where2 
select @sql = @sql + ' Order by ' + @orderCol
--获取数据集
exec (@sql)                              
select @sql2 = Replace(@sql2,'Top ' + Convert(varchar(10), @pageSize) + ' ' + @columns, 'count(1)')     
--获取总数据条数
exec(@sql2) 

Model中的输入类

public class PageParams
    {
        /// <summary>
        /// 表名、视图名
        /// </summary>
        public string TableName { get; set; }

        /// <summary>
        /// 标识列名(如:比如主键、标识,推荐使用索引列)
        /// </summary>
        public string IndexCol { get; set; }

        /// <summary>
        /// 每页多少条数据
        /// </summary>
        public int PageSize { get; set; }

        /// <summary>
        /// 当前页索引
        /// </summary>
        public int PageIndex { get; set; }

        /// <summary>
        /// 排序 (如:id desc)
        /// </summary>
        public string OrderCol { get; set; }

        /// <summary>
        /// 条件(要查询的条件)
        /// </summary>
        public string Where { get; set; }

        /// <summary>
        /// 要显示的列(要显示那些列数据)
        /// </summary>
        public string Columns { get; set; }
    }

输出类

public class Pages<T> where T:class    //T为动态的类型
    {
        /// <summary>
        /// 数据总条数
        /// </summary>
        public int SumCount { get; set; }

        /// <summary>
        /// 数据总页数
        /// </summary>
        public int SumPage { get; set; }

        /// <summary>
        /// 数据集合
        /// </summary>
        public List<T> list{ get; set; }

以上三段代码为通用分页的核心, 接下来的代码一个人为标准,总的来说就是为字段赋值,传参,获取结果

定义静态字段,

EFBll bll = new EFBll();
        static int size = 3;    //分页尺寸
        static int index = 1;   //当前页面
        static int tCount = 0;  //总计数
        static int tPage = 0;   //总页数
        static string Where = ""; //条件
public ActionResult Index()
        {
            return View(ShowBinds());
        }
        //T为动态类型,自行更改
private List<T> ShowBinds()   
        {
            string where = "1=1";
            //如果有模糊查询
            if (!string.IsNullOrEmpty(Slocation))
            {
                where += "and HouseModels.location like '%" + Slocation + "%'";
            }
            Where = where;
            PageParams page = new PageParams()
            {
                TableName = @"HouseModels  join Buildingstructuremodels on HouseModels.BuildingStructureId=Buildingstructuremodels.BuildingStructureId
                            ,   //两表联查
                PageSize = size,  //尺寸
                PageIndex = index, 
                Where = Where,
                IndexCol = "Hid",
                OrderCol = "Hid",
                Columns = "*"
            };
            Pages s = bll.ShowListAll(page);
            tCount = s.SumCount;
            tPage = s.SumPage;
            ViewBag.tCount = tCount;           
            ViewBag.SumPage = tPage;
            ViewBag.PageIndex = index;
            return s.list;
        }

接下来是数据访问层

public Pages ShowListAll(PageParams p)
        {

            SqlParameter[] paras = {
                new SqlParameter("@tableName", p.TableName),
                new SqlParameter("@indexCol", p.IndexCol),
                new SqlParameter("@pageSize", p.PageSize),
                new SqlParameter("@pageIndex", p.PageIndex - 1),
                new SqlParameter("@orderCol", p.OrderCol),
                new SqlParameter("@where", p.Where),
                new SqlParameter("@columns", p.Columns) };
                DataSet set=   DbHelperSQL.ExecuteDataset(DbHelperSQL.ConnB2c, "p_paging", paras);
                Pages page = new Pages();
                DataTable dt = set.Tables[0];
                page.SumCount = Convert.ToInt32(set.Tables[1].Rows[0][0]);
                page.SumPage = page.SumCount / p.PageSize + (page.SumCount % p.PageSize > 0 ? 1 : 0);
                page.list = JsonConvert.DeserializeObject<List<HouseModel>>(JsonConvert.SerializeObject(dt));
            return page;

        }

前台HTML

@*pagination分页,pagination-large 大小,pagination-centered 居中*@
<div class="pagination pagination-large pagination-centered">
    <ul>
        @{
            int pageindex = ViewBag.PageIndex;//当前页
            int sumpage = ViewBag.SumPage;//共多少页
            if (pageindex <= 1)
            {
                <li class="disabled"><span>首页</span></li>@*禁用*@
                <li class="disabled"><span>&laquo;</span></li>@*禁用*@
            }
            else
            {
                <li><a href="#" onclick="Pages(@(1))">首页</a></li>
                            <li><a href="#" onclick="Pages(@(pageindex - 1))">&laquo;</a></li>
            }
            int n = 4;//分页显示多少个分页格子
            int index = Convert.ToInt32(Math.Ceiling(Convert.ToDouble(pageindex) / n));
            for (int i = 1; i <= n; i++)//遍历显示分页格子
            {
                int NPage = (index - 1) * n + i;//要显示的li
                if (NPage <= sumpage)
                {
                    if (NPage == pageindex)//要显示的li等于当前要显示的页时(改变li样式)
                    {
                        <li class="active"><a href="#" onclick="Pages(@(NPage))">@(NPage)</a></li>
                    }
                    else
                    {
                        <li><a href="#" onclick="Pages(@(NPage))">@(NPage)</a></li>
                    }
                }
            }
            if (pageindex >= sumpage)
            {
                <li class="disabled"><span>&raquo;</span></li>@*禁用*@
                <li class="disabled"><span>尾页</span></li>@*禁用*@
            }
            else
            {
                <li><a href="#" onclick="Pages(@(pageindex+1))">&raquo;</a></li>
                            <li><a href="#" onclick="Pages(@(sumpage))">尾页</a></li>
            }
        }
    </ul>
</div>
<p class="text-center">共 @(ViewBag.SumPage) 页,当前为第 @(ViewBag.PageIndex) 页</p>
<script>
    function Pages(index1) {
        $.ajax({
            url: "/Main/Page",
            type: "post",
            data: { index1: index1 },
            success: function (data) {
                $("#div1").empty().append(data);
            }
        })
    }

</script>
   

这个Sql分页通用代码是无论什么项目都可以用来使用,很方便,很多东西直接用来赋值粘贴即可,例如 存储过程代码,已经 输入类及输出类, 以及前台的 分页代码

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值