熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> .NET編程 >> 正文

asp能用數據庫操作類

2022-06-13   來源: .NET編程 

  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 =
            m_pageSize =
            m_pageCount =
            m_recordCount =
            Call connect()
         End Sub
       
        private property Get GetConnectionString()
              if InStr(LCase(RequestServerVariables("SCRIPT_NAME")) "/admin/") > then
                 GetConnectionString = "Provider=microsoftjetoledb;data source=" & ServerMapPath("DataBasedatamdb")
             else
                 GetConnectionString = "Provider=microsoftjetoledb;data source=" & ServerMapPath("AdminDataBasedatamdb")
             end if
         end property
       
         public property Let SetPageSize(num)
              if Not isNumeric(num) then Exit property         
              num = CInt(num)
              if num > Then m_pageSize = num
         End property

         objectAppendSearch(paraName) = paraValue
         public property Let AppendQueryString(paraName paraValue)
            m_QueryString = m_QueryString & "&"    & paraName & "=" & ServerURLEncode(paraValue)   
         end property
       
         public property Let SetPageIndex(num)   
              if Not isNumeric(num) then Exit property               
              num = CInt(num) 
              if num > then m_pageIndex = num
         End property
       
         public default function toString()
             toString = strError
         end function
       
         private sub Connect()                
            Set m_oConn = ServerCreateObject("ADODBConnection")
            m_oConnConnectionString  = GetConnectionString
            m_oConnOpen()
            if err then
                strError = errDescription
                Responsewrite("Sorry failed to connect Database!")
                errClear()
                DeleteObject(m_oConn)
                ResponseEnd()
            end if
         end sub
       
         private sub Close(ByRef DBObject)
             if = DBObjectstate then Call DBObjectClose()
         end sub
       
         public function ExecuteScalar(strSql)
              ExecuteScalar = m_oConnExecute(strSql)()
         end function
       
         public sub Execute(ByVal strSql)
              if (Not isObject(m_oConn)) then Exit Sub
              m_oConnExecute(strSql)
         end sub
       
         private function OpenRS()
             OpenRS = false
            if ((Not isObject(m_oConn)) Or m_oConnState = ) then  exit function
           
            if (isObject(m_oRS)) then
                   if = m_oRSState then Call m_oRSClose()
            else
                Set m_oRS = ServerCreateObject("ADODBRecordSet")
            end if
            OpenRS = true            
         end function
       
         public function GetData(ByVal strSql)
            if not OpenRS() then
                GetData = null
                exit function
            end if       
            call m_oRSOpen(strSql m_oConn )
           
            if m_oRSEOF then
                GetData = null
            else
                GetData = m_oRSGetRows()                           
            end if
            m_oRSClose()   
         end function
       
         public function GetMultPageData(ByVal strSQL)
           if Not OpenRS() then
               GetMultPageData = null
               exit function
           end If
          
           Call m_oRSOpen(strSQL m_oConn )
                        
           if m_oRSEOF then
            GetMultPageData = null
           else
            m_oRSPageSize = m_pageSize
            m_recordCount = m_oRSRecordCount
            m_pageCount = m_oRSPageCount       
            if (m_pageIndex > m_pageCount) then m_pageIndex = m_pageCount
            m_oRSAbsolutePage = m_pageIndex
            GetMultPageData = m_oRSGetRows(m_pageSize)
           end if
             
           Call m_oRSClose()
         end function
       
         public function PageInfo(style)
             Dim sHtml : set sHtml = new StringBuild
             sHtmlSetValue = "<div id=""divPageNav"">"
             call sHtmlAppendFormat("<label>共:{}記錄當前:{}/{}:</label>" Array(m_recordCount m_pageIndex m_pageCount))
             Select Case style
                 Case :     if = m_pageIndex then
                                call sHtmlAppend("<span>首頁</span><span>上頁</span>")
                            else
                                call sHtmlAppendFormat("<a href=""?page={}"">首頁</a><a href=""?page={}{}"">上頁</a>" Array(m_QueryStringm_pageIndex ))
                            end if
                            if m_pageCount = m_pageIndex then
                                call sHtmlAppend("<span>下頁</span><span>尾頁</span>")
                            else
                                call sHtmlAppendFormat("<a href=""?page={}{}"">下頁</a><a href=""?page={}{}"">尾頁</a>" Array(m_QueryString m_pageIndex + m_pageCount))
                            end if
                Case :     dim interval startPage endPage i
                            interval =
                            startPage = m_pageIndex interval
                            if  startPage < then startPage =
                            endPage = startPage + * interval
                            if  endPage > m_pageCount then endPage = m_pageCount
                            startPage = endPage * interval
                            if  startPage < then startPage =
                           
                            if = m_pageIndex then
                                call sHtmlAppend("<span style=""fontfamily: Webdings;""></span><span style=""fontfamily: Webdings;""></span>")
                            else
                                call sHtmlAppendFormat("<a href=""?page={}"" style=""fontfamily: Webdings;""></a><a href=""?page={}{}"" style=""fontfamily: Webdings;""></a>" Array(m_queryString CStr(m_pageIndex )))
                            end if
                            for i = startPage to m_pageIndex
                                call sHtmlAppendFormat("<a href=""?page={}{}"">{}</a>" Array( m_queryString CStr(i)))
                            next
                            call sHtmlAppend("<span>" & m_pageIndex & "</span>")
                            for i = m_pageIndex+ to endPage
                                call sHtmlAppendFormat("<a href=""?page={}{}"">{}</a>" Array( m_queryString CStr(i)))
                            next
                           
                            if m_pageCount = m_pageIndex then
                                sHtmlAppend("<span style=""fontfamily:Webdings;""></span><span style=""fontfamily:Webdings;"">:</span>")
                            else
                                call sHtmlAppendFormat("<a href=""?page={}{}"" style=""fontfamily: Webdings;""></a><a href=""?page={}{}"" style=""fontfamily: Webdings;"">:</a>" Array(m_queryStringm_pageIndex+m_pageCount))
                            end if
             end Select
             call sHtmlAppend("</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
oDBSetPageSize =
oDBSetPageIndex = RequestQueryString("page")
arrData = oDBGetMultPageData("SELECT [TITLE] FROM [CLASS]")
for i = to ubound(arrData )
    echo(arrData( i) & "<br/>")
next
echo(oDBPageInfo())
DeleteObject(oDB)


From:http://tw.wingwit.com/Article/program/net/201311/14409.html
    推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.