发一个ASP的ADODB类代码
反正我现在用这个做了很多站,自己觉得还是满有用的,所以拿出来和大家分享一下。支持三种数据库连接: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 "数据库链接失败
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
%>
三种方法:
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 "数据库链接失败
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
%>