侯体宗的博客
  • 首页
  • Hyperf版
  • beego仿版
  • 人生(杂谈)
  • 技术
  • 关于我
  • 更多分类
    • 文件下载
    • 文字修仙
    • 中国象棋ai
    • 群聊
    • 九宫格抽奖
    • 拼图
    • 消消乐
    • 相册

发一个ASP的ADODB类代码

数据库  /  管理员 发布于 5年前   375

反正我现在用这个做了很多站,自己觉得还是满有用的,所以拿出来和大家分享一下。支持三种数据库连接: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][email protected][/email]
'        E-mail                [email][email protected][/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 t


  • 上一条:
    asp下用ADODB.Stream代替FSO读取文本文件
    下一条:
    asp下的风讯用的SQL通用防注入模块提供了
  • 昵称:

    邮箱:

    0条评论 (评论内容有缓存机制,请悉知!)
    最新最热
    • 分类目录
    • 人生(杂谈)
    • 技术
    • linux
    • Java
    • php
    • 框架(架构)
    • 前端
    • ThinkPHP
    • 数据库
    • 微信(小程序)
    • Laravel
    • Redis
    • Docker
    • Go
    • swoole
    • Windows
    • Python
    • 苹果(mac/ios)
    • 相关文章
    • 分库分表的目的、优缺点及具体实现方式介绍(0个评论)
    • DevDB - 在 VS 代码中直接访问数据库(0个评论)
    • 在ubuntu系统中实现mysql数据存储目录迁移流程步骤(0个评论)
    • 在mysql中使用存储过程批量新增测试数据流程步骤(0个评论)
    • php+mysql数据库批量根据条件快速更新、连表更新sql实现(0个评论)
    • 近期文章
    • 在go语言中使用api.geonames.org接口实现根据国际邮政编码获取地址信息功能(1个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf分页文件功能(0个评论)
    • gmail发邮件报错:534 5.7.9 Application-specific password required...解决方案(0个评论)
    • 欧盟关于强迫劳动的规定的官方举报渠道及官方举报网站(0个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf文件功能(0个评论)
    • Laravel从Accel获得5700万美元A轮融资(0个评论)
    • 在go + gin中gorm实现指定搜索/区间搜索分页列表功能接口实例(0个评论)
    • 在go语言中实现IP/CIDR的ip和netmask互转及IP段形式互转及ip是否存在IP/CIDR(0个评论)
    • PHP 8.4 Alpha 1现已发布!(0个评论)
    • Laravel 11.15版本发布 - Eloquent Builder中添加的泛型(0个评论)
    • 近期评论
    • 122 在

      学历:一种延缓就业设计,生活需求下的权衡之选中评论 工作几年后,报名考研了,到现在还没认真学习备考,迷茫中。作为一名北漂互联网打工人..
    • 123 在

      Clash for Windows作者删库跑路了,github已404中评论 按理说只要你在国内,所有的流量进出都在监控范围内,不管你怎么隐藏也没用,想搞你分..
    • 原梓番博客 在

      在Laravel框架中使用模型Model分表最简单的方法中评论 好久好久都没看友情链接申请了,今天刚看,已经添加。..
    • 博主 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 @1111老铁这个不行了,可以看看近期评论的其他文章..
    • 1111 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 网站不能打开,博主百忙中能否发个APP下载链接,佛跳墙或极光..
    • 2017-06
    • 2017-08
    • 2017-09
    • 2017-10
    • 2017-11
    • 2018-01
    • 2018-05
    • 2018-10
    • 2018-11
    • 2020-02
    • 2020-03
    • 2020-04
    • 2020-05
    • 2020-06
    • 2020-07
    • 2020-08
    • 2020-09
    • 2021-02
    • 2021-04
    • 2021-07
    • 2021-08
    • 2021-11
    • 2021-12
    • 2022-02
    • 2022-03
    • 2022-05
    • 2022-06
    • 2022-07
    • 2022-08
    • 2022-09
    • 2022-10
    • 2022-11
    • 2022-12
    • 2023-01
    • 2023-03
    • 2023-04
    • 2023-05
    • 2023-07
    • 2023-08
    • 2023-10
    • 2023-11
    • 2023-12
    • 2024-01
    • 2024-03
    Top

    Copyright·© 2019 侯体宗版权所有· 粤ICP备20027696号 PHP交流群

    侯体宗的博客