發(fā)一個(gè)ASP的ADODB類(lèi)代碼
更新時(shí)間:2007年11月02日 21:45:51 作者:
反正我現(xiàn)在用這個(gè)做了很多站,自己覺(jué)得還是滿(mǎn)有用的,所以拿出來(lái)和大家分享一下。支持三種數(shù)據(jù)庫(kù)連接:MSSQL2000,MSSQL2005,ACCESS
三種方法:
select_table(sql)
表查詢(xún),返回TRUE或FALSE
當(dāng)SQL語(yǔ)句出錯(cuò),或空記錄時(shí)返回FALSE,否則TRUE
update_table(SQL)
表更新,包括update,delete
成功執(zhí)行返回TRUE,否則返回FALSE,updated為執(zhí)行后影響記錄行數(shù)。
insert_table(sql,table,id_column)
table為表名,id_column為表中自動(dòng)編號(hào),自增字段。
當(dāng)成功執(zhí)行返回TRUE,否則返回FALSE,指定TABLE,ID_column后,將返回最后添加記錄所產(chǎn)生的自增ID。
select_table()相關(guān)方法Select_page(page,psize)
分頁(yè)處理,page為當(dāng)前頁(yè),psize為每頁(yè)記錄行數(shù)。
所有操作時(shí),自動(dòng)檢測(cè)數(shù)據(jù)庫(kù)鏈接和RS是否打開(kāi),執(zhí)行后將自動(dòng)關(guān)閉數(shù)據(jù)庫(kù)鏈接。
示例:
set db = new adodb_class
if db.select_table("select * from news order by id desc") then
page = request("page")
Select_page(page,20)'每頁(yè)20條
for i=1 to 20
response.write db.rs("title")'類(lèi)內(nèi)置rs,不可變
db.rs.movenext
if db.rs.eof then exit for
next
end if
db.rsPage = 總頁(yè)數(shù),db.nowPage= 經(jīng)過(guò)處理后當(dāng)前頁(yè),db.rsCounts數(shù)總記錄數(shù)量。
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
在頁(yè)面最尾可輸出db.readCounts 為查詢(xún)數(shù)據(jù)庫(kù)次數(shù)。
--------------------------------------------
本類(lèi)好處就是你不必?fù)?dān)心忘記關(guān)閉數(shù)據(jù)庫(kù)鏈接,不用頻繁set rs = server.recordset("adodb.recordset"),也不用set rs = nothing
缺點(diǎn)就是翻頁(yè)用的傳統(tǒng)方式。rs.absolutepage = rs.pagesize
----------------------------------------------------------
<%
'/******kshop******/
' adodb_class.asp 數(shù)據(jù)庫(kù)操作類(lèi)
' 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'數(shù)據(jù)庫(kù)類(lèi)型access,sql2000,sql2005三個(gè)值之一
dim SqlDbName,SqlUser,SqlPass,SqlServer
dim SqlAccess
dim Selectd,Insertd,Updated
dim rsCounts,rsPage,readCounts,nowPage '記錄集 總數(shù)/頁(yè)數(shù) 查詢(xún)次數(shù)
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
'********打開(kāi)數(shù)據(jù)庫(kù)鏈接******************
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 "數(shù)據(jù)庫(kù)鏈接失敗<br>sqlstring = " + connstr
Response.End()
err.clear
end if
End Sub
'**********結(jié)束/查詢(xún)構(gòu)造*************
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
'分頁(yè)處理
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語(yǔ)句,表名,自增字段
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
'關(guān)閉數(shù)據(jù)庫(kù)鏈接
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
'**********釋放類(lèi)************
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)
表查詢(xún),返回TRUE或FALSE
當(dāng)SQL語(yǔ)句出錯(cuò),或空記錄時(shí)返回FALSE,否則TRUE
update_table(SQL)
表更新,包括update,delete
成功執(zhí)行返回TRUE,否則返回FALSE,updated為執(zhí)行后影響記錄行數(shù)。
insert_table(sql,table,id_column)
table為表名,id_column為表中自動(dòng)編號(hào),自增字段。
當(dāng)成功執(zhí)行返回TRUE,否則返回FALSE,指定TABLE,ID_column后,將返回最后添加記錄所產(chǎn)生的自增ID。
select_table()相關(guān)方法Select_page(page,psize)
分頁(yè)處理,page為當(dāng)前頁(yè),psize為每頁(yè)記錄行數(shù)。
所有操作時(shí),自動(dòng)檢測(cè)數(shù)據(jù)庫(kù)鏈接和RS是否打開(kāi),執(zhí)行后將自動(dòng)關(guān)閉數(shù)據(jù)庫(kù)鏈接。
示例:
set db = new adodb_class
if db.select_table("select * from news order by id desc") then
page = request("page")
Select_page(page,20)'每頁(yè)20條
for i=1 to 20
response.write db.rs("title")'類(lèi)內(nèi)置rs,不可變
db.rs.movenext
if db.rs.eof then exit for
next
end if
db.rsPage = 總頁(yè)數(shù),db.nowPage= 經(jīng)過(guò)處理后當(dāng)前頁(yè),db.rsCounts數(shù)總記錄數(shù)量。
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
在頁(yè)面最尾可輸出db.readCounts 為查詢(xún)數(shù)據(jù)庫(kù)次數(shù)。
--------------------------------------------
本類(lèi)好處就是你不必?fù)?dān)心忘記關(guān)閉數(shù)據(jù)庫(kù)鏈接,不用頻繁set rs = server.recordset("adodb.recordset"),也不用set rs = nothing
缺點(diǎn)就是翻頁(yè)用的傳統(tǒng)方式。rs.absolutepage = rs.pagesize
----------------------------------------------------------
<%
'/******kshop******/
' adodb_class.asp 數(shù)據(jù)庫(kù)操作類(lèi)
' 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'數(shù)據(jù)庫(kù)類(lèi)型access,sql2000,sql2005三個(gè)值之一
dim SqlDbName,SqlUser,SqlPass,SqlServer
dim SqlAccess
dim Selectd,Insertd,Updated
dim rsCounts,rsPage,readCounts,nowPage '記錄集 總數(shù)/頁(yè)數(shù) 查詢(xún)次數(shù)
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
'********打開(kāi)數(shù)據(jù)庫(kù)鏈接******************
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 "數(shù)據(jù)庫(kù)鏈接失敗<br>sqlstring = " + connstr
Response.End()
err.clear
end if
End Sub
'**********結(jié)束/查詢(xún)構(gòu)造*************
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
'分頁(yè)處理
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語(yǔ)句,表名,自增字段
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
'關(guān)閉數(shù)據(jù)庫(kù)鏈接
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
'**********釋放類(lèi)************
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
%>
相關(guān)文章
結(jié)合FSO操作和Aspjpeg組件寫(xiě)的Class
這篇文章主要介紹了結(jié)合FSO操作和Aspjpeg組件寫(xiě)的Class,需要的朋友可以參考下2007-03-03創(chuàng)建一個(gè)ASP通用分頁(yè)類(lèi)
創(chuàng)建一個(gè)ASP通用分頁(yè)類(lèi)...2006-07-07EasyASP v1.5發(fā)布(包含數(shù)據(jù)庫(kù)操作類(lèi),原clsDbCtrl.asp)
EasyASP是一個(gè)方便快速開(kāi)發(fā)ASP的類(lèi),其中包含了一個(gè)數(shù)據(jù)庫(kù)控制類(lèi)(原clsDbCtrl.asp,對(duì)原代碼作了優(yōu)化和修改,包含對(duì)數(shù)據(jù)庫(kù)的各類(lèi)操作及存儲(chǔ)過(guò)程的調(diào)用,全部封裝在Easp.db中,使用起來(lái)會(huì)更方便,調(diào)用也更簡(jiǎn)單)。2008-10-10ASP類(lèi)編寫(xiě)詳細(xì)說(shuō)明
ASP類(lèi)編寫(xiě)詳細(xì)說(shuō)明...2006-08-08