发一个ASP的ADODB类代码

所属分类: 网络编程 / ASP编程 阅读数: 460
收藏 0 赞 0 分享
反正我现在用这个做了很多站,自己觉得还是满有用的,所以拿出来和大家分享一下。支持三种数据库连接:MSSQL2000,MSSQL2005,ACCESS
三种方法:
select_table(sql)
表查询,返回TRUE或FALSE
当SQL语句出错,或空记录时返回FALSE,否则TRUE
update_table(SQL)
表更新,包括update,delete
成功执行返回TRUE,否则返回FALSE,updated为执行后影响记录行数。
insert_table(sql,table,id_column)
table为表名,id_column为表中自动编号,自增字段。
当成功执行返回TRUE,否则返回FALSE,指定TABLE,ID_column后,将返回最后添加记录所产生的自增ID。

select_table()相关方法Select_page(page,psize)
分页处理,page为当前页,psize为每页记录行数。
所有操作时,自动检测数据库链接和RS是否打开,执行后将自动关闭数据库链接。

示例:
set db = new adodb_class
if db.select_table("select * from news order by id desc") then
   page = request("page")
   Select_page(page,20)'每页20条
   for i=1 to 20 
          response.write db.rs("title")'类内置rs,不可变
    db.rs.movenext
   if db.rs.eof then exit for
   next
end if
db.rsPage = 总页数,db.nowPage= 经过处理后当前页,db.rsCounts数总记录数量。

if db.update_table("delete from news where ispass=1") then'update同样
    response.write "共删除"&db.updated&"行"
end if

call db.insert_table("insert into news (title,content) values ('"&title&"','"&content&"')","news","id")
response.write "最后添加ID为"&db.Insertd

在页面最尾可输出db.readCounts 为查询数据库次数。
--------------------------------------------
本类好处就是你不必担心忘记关闭数据库链接,不用频繁set rs = server.recordset("adodb.recordset"),也不用set rs = nothing
缺点就是翻页用的传统方式。rs.absolutepage = rs.pagesize
----------------------------------------------------------
<%
'/******kshop******/
'        adodb_class.asp        数据库操作类

'        Version                1.0
'        Copyright        [email]simple_1982@hotmail.com[/email]
'        E-mail                [email]xsg2005@163.com[/email]
'/*****************/
class adodb_class
        dim conn,connstr,rs
        dim dbclass'数据库类型access,sql2000,sql2005三个值之一
        dim SqlDbName,SqlUser,SqlPass,SqlServer
        dim SqlAccess
        dim Selectd,Insertd,Updated
        dim rsCounts,rsPage,readCounts,nowPage '记录集 总数/页数 查询次数

        Private Sub Class_Initialize()
                        SqlDbName        = ""
                        SqlUser                = ""
                        SqlPass                = ""
                        SqlServer        = ""
                        SqlAccess         = "/simple_date/simple_xiehui.mdb"
                        rsCounts = 0:rsPage = 1:readCounts = 0:nowPage = 1
                        Call OpenConn("access")

                        selectd                = 0
                        Insertd                = 0
                        Updated                = 0
        End Sub
        '********打开数据库链接******************
        Private Sub AccessConn()
                        connstr="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & server.mappath(SqlAccess) &";Persist Security Info=False"
        End Sub
        Private Sub Sql2kConn()
                        connstr = "driver={SQL server};server="&SqlServer&";uid="&SqlUser&";pwd="&SqlPass&";database="&SqlDbName
        End Sub
        Private Sub Sql2k05Conn()
                        connstr="Provider=SQLNCLI.1;Password="&SqlPass&";User ID="&SqlUser&";Initial Catalog="&SqlDbName&";Data Source="&SqlServer
        End Sub
        Private Sub OpenConn(db_class)
                        dbclass = db_class
                        select case db_class
                                case "access":call AccessConn()
                                case "sql2000":call Sql2kConn()
                                case "sql2005":call Sql2k05Conn()
                        end select
                        on error resume next
                        set conn = server.CreateObject("adodb.Connection")
                        conn.open connstr
                        if err then
                                Response.write "数据库链接失败<br>sqlstring = " + connstr
                                Response.End()
                                err.clear
                        end if
        End Sub
        '**********结束/查询构造*************
        Public Function Select_Table(sql)
                        if not not isempty(conn) or isnull(conn) then
                                        call OpenConn(dbclass)
                                elseif conn.state = 0 then
                                        call OpenConn(dbclass)
                        end if
                        on error resume next
                        Set rs = Server.CreateObject("adodb.recordset")
                        rs.open sql,conn,1,1
                        if err then
                                Select_Table = False
                                rs.close
                                exit Function
                                err.clear
                        End If
                        If rs.eof and rs.bof then
                                        rs.close
                                        Select_Table = false
                                Else
                                        Select_Table = true
                        End If
                        readCounts = readCounts + 1
        End Function
        '分页处理
        Public Function Select_page(page,psize)
                        if isnull(page) or page = "" then page = 1
                        if page < 1 then page = 1
                        if rs.state = 1 then
                                if not rs.eof then
                                        rs.pagesize = psize
                                        rsPage = rs.pagecount
                                        rsCounts = rs.recordcount
                                        if int(page) > Int(rsPage) then page = rsPage
                                        rs.absolutepage = page:nowPage = page
                                end if
                        end if                
        End Function
        '更新记录
        Public Function Update_Table(Sql)
                        if not isempty(conn) or isnull(conn) then
                                        call OpenConn(dbclass)
                                elseif conn.state = 0 then
                                        call OpenConn(dbclass)
                        end if
                        on error resume next
                        if Sql <> "" then
                                conn.Execute Sql,Updated
                                if err then
                                                Update_Table = false
                                                err.clear
                                        else
                                                Update_Table = true
                                end if
                        Else
                                Update_Table = false
                        end if
                        conn.close
                        Set conn = nothing
        End Function
        '增加
        '输入:insert SQL语句,表名,自增字段
        Public Function Insert_Table(sql,table,id_column)
                        if not isempty(conn) or isnull(conn) then
                                        call OpenConn(dbclass)
                                elseif conn.state = 0 then
                                        call OpenConn(dbclass)
                        end if
                        on error resume next
                        if sql <> "" then
                                conn.Execute(sql)
                                if err then
                                                Insert_Table = false:err.clear
                                        else
                                                Insert_Table = true
                                end if
                                '获得最后增加ID
                                if table <> "" and id_column <> "" then
                                        Set ds = conn.Execute("select "&id_column&" from "&table&" order by "&id_column&" desc")
                                end if
                                if err then 
                                                Insertd = 0:err.clear
                                        else Insertd = ds(0)
                                end if
                                Set ds = nothing
                                closed()
                        else
                                Insert_Table = false
                        end if
        End Function
        '关闭数据库链接
        Public Function closed()
                        if not isempty(rs) and not isnull(rs) then
                                if rs.state = 1 then
                                        rs.close
                                end if
                        end if
                        rsCounts = 0:rsPage = 1:nowPage = 1
        end function
        '**********释放类************
        Private Sub Class_Terminate() 
                        readCounts = 0:rsCounts = 0:rsPage = 0
                        if not isempty(conn) and not isnull(conn) then
                                if conn.state = 1 then
                                        conn.close
                                end if
                                Set conn = nothing
                        end if

                        if not isempty(rs) then
                                if not isnull(rs) then
                                        if rs.state=1 then 
                                                rs.close
                                        end if
                                        set rs=nothing
                                end if                        
                        end if
        End Sub
End Class
%>
更多精彩内容其他人还在看

asp知识整理笔记4(问答模式)

这是关于asp知识整理的最后一份笔记,希望大家仔细阅读。
收藏 0 赞 0 分享

ASP基础知识VBScript基本元素讲解

这篇文章主要介绍了ASP基础知识VBScript基本元素的相关资料,需要的朋友可以参考下
收藏 0 赞 0 分享

ASP编码和解码函数详解

这篇文章主要介绍了ASP编码和解码函数的相关资料,需要的朋友可以参考下
收藏 0 赞 0 分享

ASP显示页面执行时间的方法

这篇文章主要介绍了ASP显示页面执行时间的方法,在本地测试一下输出页面需要多少时间,需要的朋友可以参考下
收藏 0 赞 0 分享

ASP基础入门第一篇(ASP技术简介)

本文将以 Active Server Pages 为中心,向你全面展示制作动态商业网站的步骤和技巧并通过大量的实例,让你在不断的理论和实践之中笑傲“网络”……
收藏 0 赞 0 分享

ASP基础入门第二篇(ASP基础知识)

这篇文章是ASP基础入门第二篇,第一篇展示了ASP动态网站设计的一些最基本的方法,相信通过实践各位对 ASP 已经有了最基本的了解,本文将进一步介绍ASP动态网站的一些基本技巧,需要的朋友可以参考下
收藏 0 赞 0 分享

ASP基础入门第三篇(ASP脚本基础)

通过前两篇的学习,相信各位已经对 ASP 的动态网站设计有了一个基本的概念和整体的印象。从本篇开始作者将从脚本语言的使用着手,由浅入深地带领大家探索 ASP 动态网站设计的真正奥秘。
收藏 0 赞 0 分享

ASP基础入门第四篇(脚本变量、函数、过程和条件语句)

大家在学习了脚本语言 VBScript 的变量、常量和过程的基本概念后,本期将继续向各位介绍 VBScript 的函数和语法。
收藏 0 赞 0 分享

ASP基础入门第五篇(ASP脚本循环语句)

在本文上两篇中,我们学习了脚本语言 VBScript 的变量、函数、过程和条件语句,本篇将继续给大家介绍 VBScipt 的循环语句,并对脚本语言在 ASP 中的应用加以总结。  
收藏 0 赞 0 分享

ASP基础入门第六篇(ASP内建对象Request)

从本篇开始作者从 ASP 内建对象着手,为大家详细剖析 ASP 的六个内建对象和各种组件的特性和方法,需要的朋友可以参考下
收藏 0 赞 0 分享
查看更多