发一个ASP的ADODB类代码
发一个ASP的ADODB类代码
发布时间:2016-12-29 来源:查字典编辑
摘要:反正我现在用这个做了很多站,自己觉得还是满有用的,所以拿出来和大家分享一下。支持三种数据库连接:MSSQL2000,MSSQL2005,AC...

反正我现在用这个做了很多站,自己觉得还是满有用的,所以拿出来和大家分享一下。支持三种数据库连接: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是否打开,执行后将自动关闭数据库链接。

示例:

setdb=newadodb_class

ifdb.select_table("select*fromnewsorderbyiddesc")then

page=request("page")

Select_page(page,20)'每页20条

fori=1to20

response.writedb.rs("title")'类内置rs,不可变

db.rs.movenext

ifdb.rs.eofthenexitfor

next

endif

db.rsPage=总页数,db.nowPage=经过处理后当前页,db.rsCounts数总记录数量。

ifdb.update_table("deletefromnewswhereispass=1")then'update同样

response.write"共删除"&db.updated&"行"

endif

calldb.insert_table("insertintonews(title,content)values('"&title&"','"&content&"')","news","id")

response.write"最后添加ID为"&db.Insertd

在页面最尾可输出db.readCounts为查询数据库次数。

--------------------------------------------

本类好处就是你不必担心忘记关闭数据库链接,不用频繁setrs=server.recordset("adodb.recordset"),也不用setrs=nothing

缺点就是翻页用的传统方式。rs.absolutepage=rs.pagesize

----------------------------------------------------------

<%

'/******kshop******/

'adodb_class.asp数据库操作类

'Version1.0

'Copyright[email]simple_1982@hotmail.com[/email]

'E-mail[email]xsg2005@163.com[/email]

'/*****************/

classadodb_class

dimconn,connstr,rs

dimdbclass'数据库类型access,sql2000,sql2005三个值之一

dimSqlDbName,SqlUser,SqlPass,SqlServer

dimSqlAccess

dimSelectd,Insertd,Updated

dimrsCounts,rsPage,readCounts,nowPage'记录集总数/页数查询次数

PrivateSubClass_Initialize()

SqlDbName=""

SqlUser=""

SqlPass=""

SqlServer=""

SqlAccess="/simple_date/simple_xiehui.mdb"

rsCounts=0:rsPage=1:readCounts=0:nowPage=1

CallOpenConn("access")

selectd=0

Insertd=0

Updated=0

EndSub

'********打开数据库链接******************

PrivateSubAccessConn()

connstr="Provider=Microsoft.Jet.OLEDB.4.0;DataSource="&server.mappath(SqlAccess)&";PersistSecurityInfo=False"

EndSub

PrivateSubSql2kConn()

connstr="driver={SQLserver};server="&SqlServer&";uid="&SqlUser&";pwd="&SqlPass&";database="&SqlDbName

EndSub

PrivateSubSql2k05Conn()

connstr="Provider=SQLNCLI.1;Password="&SqlPass&";UserID="&SqlUser&";InitialCatalog="&SqlDbName&";DataSource="&SqlServer

EndSub

PrivateSubOpenConn(db_class)

dbclass=db_class

selectcasedb_class

case"access":callAccessConn()

case"sql2000":callSql2kConn()

case"sql2005":callSql2k05Conn()

endselect

onerrorresumenext

setconn=server.CreateObject("adodb.Connection")

conn.openconnstr

iferrthen

Response.write"数据库链接失败<br>sqlstring="+connstr

Response.End()

err.clear

endif

EndSub

'**********结束/查询构造*************

PublicFunctionSelect_Table(sql)

ifnotnotisempty(conn)orisnull(conn)then

callOpenConn(dbclass)

elseifconn.state=0then

callOpenConn(dbclass)

endif

onerrorresumenext

Setrs=Server.CreateObject("adodb.recordset")

rs.opensql,conn,1,1

iferrthen

Select_Table=False

rs.close

exitFunction

err.clear

EndIf

Ifrs.eofandrs.bofthen

rs.close

Select_Table=false

Else

Select_Table=true

EndIf

readCounts=readCounts+1

EndFunction

'分页处理

PublicFunctionSelect_page(page,psize)

ifisnull(page)orpage=""thenpage=1

ifpage<1thenpage=1

ifrs.state=1then

ifnotrs.eofthen

rs.pagesize=psize

rsPage=rs.pagecount

rsCounts=rs.recordcount

ifint(page)>Int(rsPage)thenpage=rsPage

rs.absolutepage=page:nowPage=page

endif

endif

EndFunction

'更新记录

PublicFunctionUpdate_Table(Sql)

ifnotisempty(conn)orisnull(conn)then

callOpenConn(dbclass)

elseifconn.state=0then

callOpenConn(dbclass)

endif

onerrorresumenext

ifSql<>""then

conn.ExecuteSql,Updated

iferrthen

Update_Table=false

err.clear

else

Update_Table=true

endif

Else

Update_Table=false

endif

conn.close

Setconn=nothing

EndFunction

'增加

'输入:insertSQL语句,表名,自增字段

PublicFunctionInsert_Table(sql,table,id_column)

ifnotisempty(conn)orisnull(conn)then

callOpenConn(dbclass)

elseifconn.state=0then

callOpenConn(dbclass)

endif

onerrorresumenext

ifsql<>""then

conn.Execute(sql)

iferrthen

Insert_Table=false:err.clear

else

Insert_Table=true

endif

'获得最后增加ID

iftable<>""andid_column<>""then

Setds=conn.Execute("select"&id_column&"from"&table&"orderby"&id_column&"desc")

endif

iferrthen

Insertd=0:err.clear

elseInsertd=ds(0)

endif

Setds=nothing

closed()

else

Insert_Table=false

endif

EndFunction

'关闭数据库链接

PublicFunctionclosed()

ifnotisempty(rs)andnotisnull(rs)then

ifrs.state=1then

rs.close

endif

endif

rsCounts=0:rsPage=1:nowPage=1

endfunction

'**********释放类************

PrivateSubClass_Terminate()

readCounts=0:rsCounts=0:rsPage=0

ifnotisempty(conn)andnotisnull(conn)then

ifconn.state=1then

conn.close

endif

Setconn=nothing

endif

ifnotisempty(rs)then

ifnotisnull(rs)then

ifrs.state=1then

rs.close

endif

setrs=nothing

endif

endif

EndSub

EndClass

%>

推荐文章
猜你喜欢
附近的人在看
推荐阅读
拓展阅读
相关阅读
网友关注
最新ASP教程学习
热门ASP教程学习
编程开发子分类