asp能用数据库操作类。

作者:网络 来源:佚名 更新时间:2009-02-17 17:26:33 点击:

   sub deleteobject(byref obj)
        set obj = nothing
        obj = null
    end sub
    
    class cdb
        private m_oconn, m_ors
        private m_pageindex, m_pagesize, m_pagecount, m_recordcount
                                
        private strerror
        private m_querystring
        
        private sub class_initialize()
            m_oconn = null
            m_ors = null
    
            m_pageindex = 1
            m_pagesize = 10
            m_pagecount = 1
            m_recordcount = 0
            call connect()
         end sub
        
        private property get getconnectionstring()
              if instr(lcase(request.servervariables("script_name")), "/admin/") > 0 then
                 getconnectionstring = "provider=microsoft.jet.oledb.4.0;data source=" & server.mappath("database\data.mdb")
             else
                 getconnectionstring = "provider=microsoft.jet.oledb.4.0;data source=" & server.mappath("admin\database\data.mdb")
             end if
         end property
        
         public property let setpagesize(num)
              if not isnumeric(num) then exit property          
              num = cint(num)
              if num > 0 then m_pagesize = num
         end property

         'object.appendsearch(paraname) = paravalue
         public property let appendquerystring(paraname, paravalue)
            m_querystring = m_querystring & "&"    & paraname & "=" & server.urlencode(paravalue)    
         end property
        
         public property let setpageindex(num)    
              if not isnumeric(num) then exit property                
              num = cint(num)  
              if num > 0 then m_pageindex = num
         end property
        
         public default function tostring()
             tostring = strerror
         end function
        
         private sub connect()                 
            set m_oconn = server.createobject("adodb.connection")
            m_oconn.connectionstring  = getconnectionstring
            m_oconn.open()
            if err then
                strerror = err.description
                response.write("sorry, failed to connect database!")
                err.clear()
                deleteobject(m_oconn)
                response.end()
            end if
         end sub
        
         private sub close(byref dbobject)
             if 1 = dbobject.state then call dbobject.close()
         end sub
        
         public function executescalar(strsql)
              executescalar = m_oconn.execute(strsql)(0)
         end function
        
         public sub execute(byval strsql)
              if (not isobject(m_oconn)) then exit sub
              m_oconn.execute(strsql)
         end sub
        
         private function openrs()
             openrs = false
            if ((not isobject(m_oconn)) or m_oconn.state = 0) then  exit function
            
            if (isobject(m_ors)) then
                   if 1 = m_ors.state then call m_ors.close()
            else
                set m_ors = server.createobject("adodb.recordset")
            end if
            openrs = true             
         end function
        
         public function getdata(byval strsql)
            if not openrs() then
                getdata = null
                exit function
            end if        
            call m_ors.open(strsql, m_oconn, 1, 1)
            
            if m_ors.eof then
                getdata = null
            else
                getdata = m_ors.getrows()                            
            end if
            m_ors.close()    
         end function
        
         public function getmultpagedata(byval strsql)
           if not openrs() then
               getmultpagedata = null
               exit function
           end if
           
           call m_ors.open(strsql, m_oconn, 1, 1)
                         
           if m_ors.eof then
            getmultpagedata = null
           else
            m_ors.pagesize = m_pagesize
            m_recordcount = m_ors.recordcount
            m_pagecount = m_ors.pagecount        
            if (m_pageindex > m_pagecount) then m_pageindex = m_pagecount
            m_ors.absolutepage = m_pageindex
            getmultpagedata = m_ors.getrows(m_pagesize)
           end if
              
           call m_ors.close()
         end function
        
         public function pageinfo(style)
             dim shtml : set shtml = new stringbuild
             shtml.setvalue = "<div id=""divpagenav"">"
             call shtml.appendformat("<label>共:{0}记录,当前:{1}/{2}:</label>", array(m_recordcount, m_pageindex, m_pagecount))
             select case style
                 case 1 :     if 1 = m_pageindex then
                                call shtml.append("<span>首页</span><span>上页</span>")
                            else
                                call shtml.appendformat("<a href=""?page=1{0}"">首页</a><a href=""?page={1}{0}"">上页</a>", array(m_querystring,m_pageindex - 1))
                            end if
                            if m_pagecount = m_pageindex then
                                call shtml.append("<span>下页</span><span>尾页</span>")
                            else
                                call shtml.appendformat("<a href=""?page={1}{0}"">下页</a><a href=""?page={2}{0}"">尾页</a>", array(m_querystring, m_pageindex + 1, m_pagecount))
                            end if
                case 2 :     dim interval, startpage, endpage, i
                            interval = 5
                            startpage = m_pageindex - interval
                            if  startpage < 1 then startpage = 1
                            endpage = startpage + 2 * interval
                            if  endpage > m_pagecount then endpage = m_pagecount
                            startpage = endpage - 2 * interval
                            if  startpage < 1 then startpage = 1
                            
                            if 1 = m_pageindex then
                                call shtml.append("<span font-family: webdings;"">9</span><span font-family: webdings;"">7</span>")
                            else
                                call shtml.appendformat("<a href=""?page=1{0}"" font-family: webdings;"">9</a><a href=""?page={1}{0}"" font-family: webdings;"">7</a>", array(m_querystring, cstr(m_pageindex - 1)))
                            end if
                            for i = startpage to m_pageindex-1
                                call shtml.appendformat("<a href=""?page={1}{0}"">{1}</a>", array( m_querystring, cstr(i)))
                            next
                            call shtml.append("<span>" & m_pageindex & "</span>")
                            for i = m_pageindex+1 to endpage
                                call shtml.appendformat("<a href=""?page={1}{0}"">{1}</a>", array( m_querystring, cstr(i)))
                            next
                            
                            if m_pagecount = m_pageindex then
                                shtml.append("<span font-family:webdings;"">8</span><span font-family:webdings;"">:</span>")
                            else
                                call shtml.appendformat("<a href=""?page={1}{0}"" font-family: webdings;"">8</a><a href=""?page={2}{0}"" font-family: webdings;"">:</a>", array(m_querystring,m_pageindex+1,m_pagecount))
                            end if
             end select
             call shtml.append("</div>")
             pageinfo = shtml
             deleteobject(shtml)
         end function
        
         private sub class_terminate()
              close(m_ors)
            close(m_oconn)
            deleteobject(m_ors)
            deleteobject(m_oconn)
         end sub
    end class
    
    dim i, odb, arrdata : set odb = new cdb
odb.setpagesize = 2
odb.setpageindex = request.querystring("page")
arrdata = odb.getmultpagedata("select [title] from [class]")
for i = 0 to ubound(arrdata, 2)
    echo(arrdata(0, i) & "<br/>")
next
echo(odb.pageinfo(1))
deleteobject(odb)