虽然现在做PHP了,但是对ASP还是放不太下,最近两天想起写个CMS,于是就开始从一些基本的底层操作写起,最底层的当然是数据库连接,为了使系统更加安全和健壮以及更有扩展性,于是决定用类来写。以下是我写的一个关于数据库的通用连接类,由于本人技术有限,可能有不对的地方,还希望大家能提出来,恳请大家能抽出一点点时间把里面不好的地方,或者是你认为有更好的实现方法通过Email或者在评论里通知我一下,谢谢。同时也希望我写的这个对某些人有所帮助。
<%
'#############################################################
'###ASP通用数据库操作类
'###copyright by wjazz
'###POWER BY 2008-07-26
'###QQ:21765453 MSN:ball-202@tom.com
'#############################################################
'### 使用说明(ACCESS数据库例子)
'### dim db
'### set db=new DB_Class
'### db.OpenAccess("data.mdb") '其他数据库按照参数设置就可以了
'### 查询数据库
'### set rs=db.Query("select * from table")
'### if not rs.bof and not rs.eof then
'### do while not rs.eof
'### ..................
'### rs.movenext
'### loop
'### ned if
'### rs.close
'### set rs=nothing
'### 插入数据
'### countnum=db.QueryNone("INSERT INTO table(field1,field2,...) values(value1,value2,...)")
'### if cint(countnum)>0 then
'### response.write("数据增加成功,共影响行数:"&countnum)
'### else
'### response.write("数据增加失败")
'### end if
'### 修改数据
'### countnum=db.QueryNone("UPDATE table set .......")
'### if cint(countnum)>0 then
'### response.write("数据修改成功,共影响行数:"&countnum)
'### else
'### response.write("数据修改失败")
'### end if
'### 删除数据
'### countnum=db.QueryNone("DELETE from table where ...")
'### if cint(countnum)>0 then
'### response.write("数据删除成功,共影响行数:"&countnum)
'### else
'### response.write("数据删除失败")
'### end if
'### set db=nothing '关闭类
'#############################################################
Class DB_Class
'###数据连接私有变量申明
'#数据库连接对象
'# @Object
private conn
'#数据连接串
'# @string
private conn_str
'#错误消息
'# @string
private err_str
'#开始时间
'# @date
private start_time
'#执行的SQL总数
'# @int
private sql_count
'#出现错误是否停止程序
'# @boolean
private err_stop
'###构造函数
private sub Class_initialize()
err_str="" '#初始化错误信息
sql_count=0
err_stop=false
start_time=timer()
end sub
'#####类关闭函数
private sub class_terminate()
if isobject(conn) then
set conn=nothing
end if
end sub
'###设置出现错误是否停止运行程序(默认为否)
public property let Get_errstop(bool_stop)
err_stop=bool_stop
end property
'###连接ACCESS数据库
public sub OpenAccess(dbpath)
conn_str="Provider=Microsoft.Jet.OLEDB.4.0;Data Source="&Server.MapPath(dbpath)
open()
end sub
'###连接MSSQL数据库
public sub OpenMssql(Host,DbName,User,PassWord)
conn_str="Provider=SQLOLEDB.1;Persist Security Info=True;User ID="&User&";Password="&PassWord&";Initial Catalog="&DbName&";Data Source="&Host
open()
end sub
'###连接Oracle数据库
public sub OpenOracle(Host,User,PassWord)
conn_str="Provider=MSDAORA.1;Persist Security Info=True;User ID="&User&";Password="&PassWord&";Data Source="&Host
open()
end sub
'###连接Excel数据库
public sub OpenExcel(dbpath)
conn_str="Provider=Microsoft.Jet.OLEDB.4.0;Data Source="&server.mappath(dbpath)&";Extended Properties=""Excel 8.0;"""
open()
end sub
'###连接数据库 私有函数
private function open()
on error resume next
set conn=server.createobject("ADODB.connection")
if err.Number<>0 then
err.clear
err_str=err_str&"建立connection对象失败!<br />"
if err_stop=true then
response.end()
end if
else
conn.open conn_str
if err.Number<>0 then
err.clear
err_str=err_str&"数据库连接错误!<br />"
if err_stop=true then
response.end()
end if
end if
end if
end function
'###关闭连接
public sub Close()
if isobject(conn) then
set conn=nothing
end if
end sub
'#运行时间显示
public sub Get_Runtime()
dim end_time,runtime
end_time=timer()
runtime=cstr(cint(formatnumber((end_time-start_time)*1000,3)))&"ms"
response.write "共花费时间:["&runtime &"]"
end sub
'###打印错误信息
public sub printerr()
response.write "<br />错误信息:"&err_str
end sub
'####执行SQL,返回一个rs对象(select)
public function Query(sql)
sql_count=sql_count+1
on error resume next
set Query=Server.createobject("ADODB.recordset")
Query.open sql ,conn,1,1
if err.Number<>0 then
err.clear
err_str=err_str&"数据查询出错:"&sql&"<br />"
set Query=nothing
if err_stop then
response.end()
end if
end if
end function
'执行SQL,返回SQL所影响的行数(insert、update、delete)
public function QueryNone(sql)
dim rs_affected
sql_count=sql_count+1
on error resume next
conn.execute sql,rs_affected
QueryNone=rs_affected
if err.Number<>0 then
err_str=err_str&"数据更新出错:"&sql&"<br />"
err.clear
if err_stop then
response.end()
end if
end if
end function
End Class
%>