主页 > 知识库 > 浅谈mssql access数据库 top分页方法

浅谈mssql access数据库 top分页方法

热门标签:贵州外呼回拨系统是什么 智能外呼系统需要多少钱 外呼系统的话术 智能语音车载电话机器人 阳光创信ai外呼获客系统助力 云南全自动外呼系统公司 卫星地图标注距离 安阳天音防封电销卡 竞圣地图标注服务

但是有利也有弊,它要求排序字段必须构成唯一记录,且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);
    }
}

您可能感兴趣的文章:
  • Access 2000 数据库 80 万记录通用快速分页类
  • Asp.net在线备份、压缩和修复Access数据库示例代码
  • ASP.NET 连接ACCESS数据库的简单方法
  • asp.net中获取新增加记录的ID Access版
  • asp.net下Oracle,SQL Server,Access万能数据库通用类
  • 一个简答的Access下的分页asp.net代码
  • asp.net访问Access数据库溢出错误
  • asp.net(C#) Access 数据操作类
  • asp.net access web.config denied
  • asp.net 数据库备份还原(sqlserver+access)
  • asp.net access添加返回自递增id的实现方法
  • asp.net和asp下ACCESS的参数化查询
  • asp.net实现access数据库分页的方法

标签:周口 预约服务 营口 湘潭 鄂尔多斯 河源 宁夏 钦州

巨人网络通讯声明:本文标题《浅谈mssql access数据库 top分页方法》,本文关键词  浅谈,mssql,access,数据库,;如发现本文内容存在版权问题,烦请提供相关信息告之我们,我们将及时沟通与处理。本站内容系统采集于网络,涉及言论、版权与本站无关。
  • 相关文章
  • 下面列出与本文章《浅谈mssql access数据库 top分页方法》相关的同类信息!
  • 本页收集关于浅谈mssql access数据库 top分页方法的相关信息资讯供网民参考!
  • 推荐文章