萬盛學電腦網

 萬盛學電腦網 >> 網絡編程 >> asp編程 >> ASP教程:自己寫的數據庫操作類

ASP教程:自己寫的數據庫操作類

程序代碼:

以下為引用的內容:
<%
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數據庫時設置以下兩個變量
        'dim dbServer            '數據庫服務器的名稱或ip地址
        'dim dbname              '數據庫的名字
        dim dbPath              '若是Access數據庫,此處設置其路徑
        dim dbUser              '數據庫的登錄用戶名
        dim dbPass              '數據庫的登錄密碼
        dim connstr
        dbPath = "/testasp/data/data.mdb" '設置數據庫路徑
        dbUser = "admin"
        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
        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 propert

copyright © 萬盛學電腦網 all rights reserved