asp能用数据库操作类

2015-08-12 16:04:55来源:作者:人点击

  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("DataBasedata.mdb")
             else
                 GetConnectionString = "Provider=microsoft.jet.oledb.4.0;data source=" & Server.MapPath("AdminDataBasedata.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 style=""font-family: Webdings;"">9</span><span style=""font-family: Webdings;"">7</span>")
                            else
                                call sHtml.AppendFormat("<a href=""?page=1{0}"" style=""font-family: Webdings;"">9</a><a href=""?page={1}{0}"" style=""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 style=""font-family:Webdings;"">8</span><span style=""font-family:Webdings;"">:</span>")
                            else
                                call sHtml.AppendFormat("<a href=""?page={1}{0}"" style=""font-family: Webdings;"">8</a><a href=""?page={2}{0}"" style=""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)

最新文章

123

最新摄影

微信扫一扫

第七城市微信公众平台