当前位置: 移动技术网 > IT编程>开发语言>Asp > 查询翻页优化第1/2页

查询翻页优化第1/2页

2017年12月12日  | 移动技术网IT编程  | 我要评论
复制代码 代码如下:

<script language=vbscript runat=server>

'//---- cocoon_自定义类_翻页优化代码 ----//'
class cc_db_pager
    '--------------------------------
    '     cocoon db_pager 类 (ver: 1.02)
    '     作者: sunrise_chen (sunrise_chen@msn.com)
    '     请保留此信息,谢谢。
    '     
    '     2003/06 修正一个错误,描述:如果id不唯一,将产生多条重复记录的错误。
    '                           2003.5
    '--------------------------------

    '//-------------------- 定义变量 --------------------//'
    private stablename        '//表名
    private ssqlstring        '//自定义sql语句
    private acondition()        '//查询条件(数组)
    private scondition        '//查询条件(字符串)
    private ipage        '//当前页码
    private ipagesize        '//每页记录数
    private ipagecount        '//总页数
    private ireccount        '//当前查询条件下的记录数
    private itotalreccount    '//总记录数
    private sfields        '//输出的字段名
    private sorderby        '//排序字符串
    private ssql        '//当前的查询语句
    private spkey        '//主键
    private oconn        '//连接对象
    private idefpagesize        '//默认每页显示的记录数
    private sprojectname        '/项目名
    private sversion        '/版本号
    private bshowerror        '//是否显示错误信息

    '//-------------------- 事件、方法 --------------------//'
    '//类初始化事件
    private sub class_initialize()
        redim acondition(-1)
        ipage        = 1
        ireccount        = null
        itotalreccount    = null
        ipagecount        = null
        bshowerror        = true
        ipagesize        = 10
        sfields        = "*"
        spkey        = "id"
        scondition        = ""
        sorderby        = ""
        ssqlstring        = ""
        sprojectname    = "cocoon 类系列 数据库翻页优化"
        sversion        = "1.02"
    end sub

    '//类结束事件
    private sub class_terminate()
        set oconn = nothing
    end sub

    '//错误处理
    public sub doerror(s)
            dim stmp
            stmp = clng(rnd() * 100)
            response.write( "<div style='width:760;font-size:9pt;cursor:hand'>" )
            response.write( "<label onclick='errordiv"&stmp&".style.display=(errordiv"&stmp&".style.display==""""?""none"":"""")'>" )
            response.write( "<span style='background-color:#cccc00;color:white;'>〖 cc_db_pager 提示信息 〗</span><br></label>" )
            response.write( "<div id='errordiv"&stmp&"' style='display:none;width:100%;" )
            response.write( "border: 1px solid #cccc00;padding:5;overflow:hidden;text-overflow:ellipsis;'><nobr>" )
            response.write( "<span style='color:red'>description</span>: " & s & "<br>" )
            response.write( "<span style='color:red'>provider</span>: " & sprojectname )
            response.write( "  <span style='color:red'>version</span>: " & sversion & "<br>" )
            response.write( "</nobr></div></div><br>" )
    end sub

    '//产生分页的sql语句
    public function getsql()
        dim istart, iend
        call makecondition()
        istart = ( ipage - 1 ) * ipagesize
        iend = istart + ipagesize
        getsql  = " select distinct " & sfields & " from ["&stablename&"] " _
                & " where ["&spkey&"] not in ( " _
                & "   select top "&istart&" ["&spkey&"] from ["&stablename&"] " & scondition & " " & sorderby & " " _
                & " )" _
                & " and ["&spkey&"] in ( " _
                & "   select top "&iend&" ["&spkey&"] from ["&stablename&"] " & scondition & " " & sorderby & " " _
                & " )" _
                & " " & sorderby & " "
    end function

    '//产生条件字符串
    private sub makecondition()
        if ubound(acondition)>=0 then
            scondition = " where " & join(acondition, " and ")
        end if
    end sub

    '//计算总记录数(带条件)
    private sub caculatereccount()
        on error resume next
        dim ors
        call makecondition()
        set ors = oconn.execute( "select count(["&spkey&"]) from [" & stablename & "]" & scondition )
        if err then
            doerror err.description
            response.end()
        end if
        ireccount = ors.fields.item(0).value
        set ors = nothing
    end sub

    '//计算总记录数(不带条件)
    private sub caculatetotalreccount()
        on error resume next
        dim ors
        set ors = oconn.execute( "select count(["&spkey&"]) from [" & stablename & "]" )
        if err then
            doerror err.description
            response.end()
        end if
        itotalreccount = ors.fields.item(0).value
        set ors = nothing
    end sub

    '//计算页数
    private sub caculatepagecount()
        if isnull(ireccount) then caculatereccount()
        if ireccount = 0 then ipagecount = 0 : exit sub
        ipagecount = abs( int( 0 - (ireccount / ipagesize) ) )
    end sub

    '//增加条件
    public sub addcondition(s)
        if len(s)<0 then exit sub
        redim preserve acondition(ubound(acondition)+1)
        acondition(ubound(acondition)) = s
    end sub

    '//版本信息
    public function information()
        doerror "coding by <a href='mailto:sunrise_chen@msn.com'>sunrise_chen</a> @ <a href='http://www.ccopus.com'>http://www.ccopus.com</a> ."
    end function


    '//-------------------- 输入属性 --------------------//'
    '//定义连接对象
    public property set activeconnection(o)
        set oconn = o
    end property

    '//定义查询表名
    public property let tablename(s)
        stablename = s
    end property

    '//定义需要输出的字段名
    public property let fields(s)
        sfields = s
    end property

    '//定义主键
    public property let pkey(s)
        spkey = s
    end property

    '//定义排序规则
    public property let orderby(s)
        sorderby = " order by " & s & " "
    end property

    '//定义每页的记录条数 
    public property let pagesize(s)
        ipagesize = s
        if not isnumeric(ipagesize) then ipagesize = idefaultpagesize
        if clng(ipagesize)<1 then ipagesize = idefaultpagesize
    end property

    '//定义当前页码
    public property let page(s)
        ipage = s
        if not isnumeric(ipage) then ipage = 1
        if clng(ipage)<1 then ipage = 1
        call caculatepagecount()
        if clng(ipage)>clng(ipagecount) and ipagecount>0 then ipage = ipagecount
    end property

    '//自定义查询语句
    public property let sql(s)
        ssqlstring = s
    end property

    '//-------------------- 输出属性 --------------------//'
    '//取得当前条件下的记录数
    public property get recordcount
        if isnull(ireccount) then caculatereccount()
        recordcount = ireccount
    end property

    '//取得当前页码
    public property get page
        page = ipage
    end property

    '//取得当前页码
    public property get absolutepage
        absolutepage = ipage
    end property

    '//取得当前查询的条件
    public property get condition
        if len(scondition)<1 then makecondition()
        condition = scondition
    end property

    '//取得总的记录数
    public property get totalrecordcount
        if isnull(itotalreccount) then caculatetotalreccount()
        totalrecordcount = itotalreccount
    end property

    '//取得总页数
    public property get pagecount
        if isnull(ipagecount) then caculatepagecount()
        pagecount = ipagecount
    end property

    '//得到分页后的记录集
    public property get recordset
        on error resume next
        ssql = getsql()
        set recordset = oconn.execute( ssql )
        if err then
            if bshowerror then doerror err.description
            if len(ssqlstring)>0 then
                set recordset = oconn.execute( ssqlstring )
                if err then
                    doerror err.description
                    response.end()
                end if
            else
                doerror err.description
            end if
        end if
        err.clear()
    end property

    '//版本信息
    public property get version
        version = sversion
    end property

end class

</script>

1

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网