以下是代碼: <% Class dbClass ’------------------------------------------------------------------------- ’變數說明 ’conn-----------connection對象 ’strsql---------執行查詢的語句 ’vTbName--------查詢分頁的表名 ’vPKey----------查詢分頁的表的主鍵 ’vPgFields------查詢分頁要顯示的欄位 ’vPgSize--------查詢分頁每頁顯示的記錄數 ’vCurrPg--------查詢分頁顯示的當前頁 ’vConditions----查詢分頁的條件 ’vOrderBy-------查詢分頁的排序 ’------------------------------------------------------------------------- private conn,strsql,vTbName,vPKey,vPgFields,vPgSize,vCurrPg,vConditions,vOrderBy ’類的初始化 private Sub Class_Initialize() ’當是MS Sql資料庫時設定以下兩個變數 Webjx.Com ’dim dbServer ’資料庫伺服器的名稱或ip地址 ’dim dbname ’資料庫的名字 dim dbPath ’若是Access資料庫,此處設定其路徑 dim dbUser ’資料庫的登入使用者名稱 dim dbPass ’資料庫的登入密碼 dim connstr dbPath = "/testasp/data/data.mdb" ’設定資料庫路徑 dbUser = "admin" Webjx.Com
dbPass = "123456" ’若是access,並且有密碼 connstr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Server.MapPath(dbPath) &_ ";User ID=" & dbUser & ";Password=;Jet OLEDB:Database Password=" & dbPass ’若是access,並且沒有密碼 ’connstr = "Provider = Microsoft.Jet.OLEDB.4.0;Data Source = " & Server.MapPath(dbPath) ’若是ms-sql資料庫 ’connstr = "Provider = Sqloledb; User ID = " & dbUser & "; Password = " & dbPass &_ ’ "; Initial Catalog = " & dbname & "; Data Source = " & dbServer Webjx.Com
on error resume next set conn=server.CreateObject("adodb.connection") conn.open connstr errMsg "串連資料庫" End Sub ’類結束 Private Sub Class_terminate() conn.close set conn=nothing End Sub ’------------------------------------------------------------------------- ’給類的變數設定值 ’------------------------------------------------------------------------- ’設定sql語句 Public Property Let sqlStr(Byval Values) strsql=Values End Property ’設定查詢分頁的表名 public property let tbName(Byval Values)
vTbName=Values end property ’-------------------------------------------------------- ’設定查詢分頁的表的主鍵 public property let pKey(ByVal Values) vPKey=Values end property ’-------------------------------------------------------- ’設定顯示的欄位 public property let pgFields(ByVal Values) vPgFields=Values end property ’-------------------------------------------------------- ’設定每頁顯示的記錄數 public property let pgSize(ByVal Values) vPgSize=Values end property ’--------------------------------------------------------- ’設定當前顯示的頁數 public property let currPg(ByVal Values) vCurrPg=Values end property ’-------------------------------------------------------- ’設定查詢的條件 public property let conditions(ByVal Values) if Len(Values)>0 then vConditions=" where "&Values else vConditions=" where 1=1 " end if end property ’------------------------------------------------------- ’設定查詢的排序 public property let orderBy(ByVal Values) if Len(Values)>0 then vOrderBy=" order by "&Values else vOrderBy=Values end if end property ’------------------------------------------------------------- 網頁教學網 ’得到記錄總數 public property get vRsCount() if vCurrPg=1 then sqlc="select count("&vPKey&") as Idcount from "&vTbName&" "&vConditions set rsc=server.CreateObject("adodb.recordset") rsc.open sqlc,conn,0,1 RsNum=rsc("IdCount") rsc.close set rsc=nothing if RsNum>0 then response.Cookies("iRecord")=RsNum vRsCount=RsNum else vRsCount=0 end if else vRsCount=request.Cookies("iRecord") end if end property ’得到總頁數 public property get vPgCount() 網頁教學網
iRsCount2=vRsCount() if iRsCount2 mod vPgSize =0 then vPgCount=int(iRsCount2/vPgSize) else vPgCount=int(iRsCount2/vPgSize)+1 end if end property ’查詢資料庫 Public Function rsDB() on error resume next ’簡單的查詢出結果 ’ set rsDB = Server.CreateObject("ADODB.RecordSet") ’ rsDB.Open strsql,conn,1,3 Set rsDB=conn.Execute(strsql) errMsg "查詢資料庫" End Function ’添加,更新,刪除資料庫記錄 public Function upDB() on error resume next conn.execute(strsql) errMsg "編輯資料庫記錄" end Function ’------------------------------------------------------------------------- Webjx.Com ’用來實現分頁的記錄集函數 public function pageRs() on error resume next dim startRs startRs=(vCurrPg-1)*vPgSize ’------------------------------------------------------------------------------------------- ’使用此語句的話要根據參數修改代碼,具體的是若排序為asc則<改為>,min改為max ’ if startRs=0 then ’ strsql="select top "&vPgSize&" "&vPgFields&" from "&vTbName&" "&vConditions&" "&vOrderBy ’ else ’ strsql="select top "&vPgSize&" "&vPgFields&" from "&vTbName&" "&vConditions&" and "&vPKey&" < " ’ strsql=strsql&"(select min("&vPKey&") from (select top "&startRs&" "&vPKey&" from " Webjx.Com ’ strsql=strsql&vTbName&" "&vConditions&" "&vOrderBy&") as idTable) "&vOrderBy ’ end if ’--------------------------------------------------------------- if startRs=0 then strsql="select top "&vPgSize&" "&vPgFields&" from "&vTbName&" "&vConditions&" "&vOrderBy else strsql="select top "&vPgSize&" "&vPgFields&" from "&vTbName&" "&vConditions&" and "&vPKey&" not " strsql=strsql&"in (select top "&startRs&" "&vPKey&" from "&vTbName&" "&vConditions&" "&vOrderBy strsql=strsql&") "&vOrderBy Webjx.Com end if ’------------------------------------------------------------------- set pageRs=server.CreateObject("adodb.recordset") pageRs.open strsql,conn,0,1 errMsg "記錄分頁" end function ’------sql用預存程序分頁------------------------------------------------------ public function sqlPage() on error resume next Set sqlPage=server.CreateObject("Adodb.RecordSet") Set Cm=Server.CreateObject("Adodb.Command") Cm.CommandType = 4 Cm.ActiveConnection = conn Cm.CommandText="sp_Util_Page" Cm.parameters(1) = vPgFields Cm.parameters(2) = vTbName Cm.parameters(3) = vConditions Cm.parameters(4) = vOrderBy Cm.parameters(5) = vPKey Cm.parameters(6) = vCurrPg Cm.parameters(7) = vPgSize Cm.parameters(8) = vRsCount() Cm.parameters(9) = "" sqlPage.CursorLocation = 3 Webjx.Com
sqlPage.LockType = 1 sqlPage.Open Cm errMsg "記錄分頁" end function ’---------------------------------------------------------------------------- ’關閉記錄集objRs ’---------------------------------------------------------------------------- Public Function cRs(ByVal ObjRs) ObjRs.close() Set ObjRs = Nothing End Function ’----------------------分頁的頁碼導航--------------------------------------- public function pageNav() iRsCount=vRsCount()’總記錄數 mypage=vCurrPg’當前頁數 PgCount=vPgCount()’總頁數 prePage=mypage-1 if prePage<1 then prePage=1 end if nextPage=mypage+1 if nextPage>PgCount then nextPage=PgCount end if pagestr="<div id=""fy""><span id=""rpc"">總共有"&iRsCount&"條記錄 "&mypage&"/"&PgCount&"</span>" Webjx.Com pagestr=pagestr&"<a href=’?currpage=1’ class=’aW’>首頁</a><a href=’?currpage="&prePage&"’ class=’aW’>前一頁</a>" if (mypage-1) mod 4=0 then firstPage=mypage elseif int((mypage-1)/4)=0 then firstPage=1 else firstPage=int((mypage-1)/4)*4+1 end if endPage=firstPage+4 astr="" for i=firstPage to endPage astr=astr&"<a href=’?currpage="&i&"’" if Cstr(mypage)=Cstr(i) then astr=astr&" id=’currP’" end if astr=astr&">"&i&"</a>" if i>PgCount-1 then exit for next astr=astr&"<a href=’?currpage="&nextPage&"’ class=’aW’>後一頁</a><a href=’?currpage="&PgCount&"’ class=’aW’>尾頁</a></div>" pagestr=pagestr&astr pageNav=pagestr end function ’輸出帶分頁功能的table Function showTb(ByVal TbTil) Webjx.Com set rsTb=pageRs()’若是預存程序就調用sqlPage() tbRs= rsTb.getrows() cRs(rsTb) iTblRow=Ubound(tbRs,2) iTblCol=Ubound(TbTil) tbStr="<table border=’0’ cellspacing=’0’ cellpadding=’0’><tbody>" for r1=0 to iTblCol tr1=tr1&"<td width=’"&split(TbTil(r1),"|")(1)&"’>"&split(TbTil(r1),"|")(0)&"</td>" next tr1="<tr>"&tr1&"</tr>" for ri=0 to iTblRow for ci=0 to iTblCol td=td&"<td width=’"&split(TbTil(ci),"|")(1)&"’>"&tbRs(ci,ri)&"</td>" next
tr=tr&"<tr>"&td&"</tr>" td=null next TbTil=null tbRs=null response.Write(tbStr&tr1&tr&"<tr><td colspan=’"&iTblCol+1&"’>"&pageNav()&"</td></tr></tbody></table>") tbStr=null tr1=null tr=null End Function ’列印sql語句,以便語句有錯誤時檢查 Public Sub prnSql() response.Write(strsql) End Sub ’------------------------------------------------------------------------- ’容錯函數 ’------------------------------------------------------------------------- Private Function errMsg(errMsg)
If Err.number<>0 Then ’出現問題可利用此處代碼列印出描述資訊,方便調試。可注釋掉 response.Write(Cstr(Err.description)&"<br>") Err.Clear Response.Write "<font color=’#FF0000’>"&errMsg&"出錯</font>" ’注釋 Response.End() End If End Function ’------------------------------------------------------------------------- ’容錯函數結束 ’------------------------------------------------------------------------- End Class %> |