一个ACCESS数据库访问的类第1/3页
大部分asp应用,都离不开对数据库的访问及操作,所以,对于数据库部分的访问操作,我们应该单独抽象出来,封装成一个单独的类。如果所用语言支持继承,可以封装一个这样的类,然后在数据操作层继承即可。下面是我写的一个access数据库访问的类,针对access作了优化,不过因为缺少足够的应用测试,可能仍然存在未知的bug及应用限制,主要代码如下:
<%
class oledb private idatapath
private iconnectionstring private conn
private cmd
private param
private rs public property let datapath(byval value)
idatapath = value
iconnectionstring = "provider = microsoft.jet.oledb.4.0;data source = " & server.mappath(idatapath)
end property public property get datapath()
datapath = idatapath
end property public property let connectionstring(byval value)
iconnectionstring = value
end property public property get connectionstring()
connectionstring = iconnectionstring
end property public function openconn()
if conn.state = adstateclosed then
conn.open connectionstring
end if
set openconn = conn
end function public function insert(byval sql, byval values)
openconn()
rs.open sql, conn, 3, 3, adcmdtext
rs.addnew
dim i, l
l = ubound(values)
for i = 1 to l + 1
rs(i) = values(i - 1)
next
rs.update
insert = rs(0)
end function public function execute(byval sql)
openconn()
set execute = conn.execute(sql)
end function public function executescalar(byval sql)
dim irs : set irs = execute(sql)
if not irs.bof then executescalar = irs(0)
end function public function executenonquery(byval sql)
openconn()
call conn.execute(sql, executenonquery)
end function public function insertsp(byval sql, byval params)
openconn()
rs.open sql, conn, 3, 3, adcmdstoredproc
rs.addnew
dim i, l
l = ubound(params)
for i = 1 to l + 1
rs(i) = params(i - 1)
next
rs.update
insertsp = rs(0)
end function public function executesp(byval spname, byval params)
with cmd
set .activeconnection = openconn()
.commandtext = spname
.commandtype = &h0004
.prepared = true
set executesp = .execute(,params)
end with
end function public function executedatatablesp(byval spname, byval params)
openconn()
if rs.state <> adstateclose then
rs.close()
end if
dim spstr
if isnull(params) or isempty(params) then
spstr = spname
else
if isarray(params) then
spstr = "execute " & spname & " " & join(params, ",")
else
spstr = "execute " & spname & " " & params
end if
end if
call rs.open(spstr, conn, 1, 1, adcmdstoredproc)
set executedatatablesp = rs
end function public function executescalarsp(byval spname, byval params)
dim irs : set irs = executesp(spname, params)
if not irs.bof then executescalarsp = irs(0)
end function public function executenonquerysp(byval spname, byval params)
with cmd
set .activeconnection = openconn()
.commandtext = spname
.commandtype = &h0004
.prepared = true
call .execute(executenonquerysp, params)
end with
end function private sub class_initialize()
set conn = server.createobject("adodb.connection")
set cmd = server.createobject("adodb.command")
set param = server.createobject("adodb.parameter")
set rs = server.createobject("adodb.recordset")
datapath = "/data/data.mdb" '这里写你的数据库默认路径,建议更改名称及扩展名
end sub
private sub class_terminate()
set param = nothing
set cmd = nothing
closers()
closeconn()
end sub private sub closeconn()
if conn.state <> adstateclose then
conn.close()
set conn = nothing
end if
end sub private sub closers()
if rs.state <> adstateclose then
rs.close()
set rs = nothing
end if
end sub end class
%>
<%
class oledb private idatapath
private iconnectionstring private conn
private cmd
private param
private rs public property let datapath(byval value)
idatapath = value
iconnectionstring = "provider = microsoft.jet.oledb.4.0;data source = " & server.mappath(idatapath)
end property public property get datapath()
datapath = idatapath
end property public property let connectionstring(byval value)
iconnectionstring = value
end property public property get connectionstring()
connectionstring = iconnectionstring
end property public function openconn()
if conn.state = adstateclosed then
conn.open connectionstring
end if
set openconn = conn
end function public function insert(byval sql, byval values)
openconn()
rs.open sql, conn, 3, 3, adcmdtext
rs.addnew
dim i, l
l = ubound(values)
for i = 1 to l + 1
rs(i) = values(i - 1)
next
rs.update
insert = rs(0)
end function public function execute(byval sql)
openconn()
set execute = conn.execute(sql)
end function public function executescalar(byval sql)
dim irs : set irs = execute(sql)
if not irs.bof then executescalar = irs(0)
end function public function executenonquery(byval sql)
openconn()
call conn.execute(sql, executenonquery)
end function public function insertsp(byval sql, byval params)
openconn()
rs.open sql, conn, 3, 3, adcmdstoredproc
rs.addnew
dim i, l
l = ubound(params)
for i = 1 to l + 1
rs(i) = params(i - 1)
next
rs.update
insertsp = rs(0)
end function public function executesp(byval spname, byval params)
with cmd
set .activeconnection = openconn()
.commandtext = spname
.commandtype = &h0004
.prepared = true
set executesp = .execute(,params)
end with
end function public function executedatatablesp(byval spname, byval params)
openconn()
if rs.state <> adstateclose then
rs.close()
end if
dim spstr
if isnull(params) or isempty(params) then
spstr = spname
else
if isarray(params) then
spstr = "execute " & spname & " " & join(params, ",")
else
spstr = "execute " & spname & " " & params
end if
end if
call rs.open(spstr, conn, 1, 1, adcmdstoredproc)
set executedatatablesp = rs
end function public function executescalarsp(byval spname, byval params)
dim irs : set irs = executesp(spname, params)
if not irs.bof then executescalarsp = irs(0)
end function public function executenonquerysp(byval spname, byval params)
with cmd
set .activeconnection = openconn()
.commandtext = spname
.commandtype = &h0004
.prepared = true
call .execute(executenonquerysp, params)
end with
end function private sub class_initialize()
set conn = server.createobject("adodb.connection")
set cmd = server.createobject("adodb.command")
set param = server.createobject("adodb.parameter")
set rs = server.createobject("adodb.recordset")
datapath = "/data/data.mdb" '这里写你的数据库默认路径,建议更改名称及扩展名
end sub
private sub class_terminate()
set param = nothing
set cmd = nothing
closers()
closeconn()
end sub private sub closeconn()
if conn.state <> adstateclose then
conn.close()
set conn = nothing
end if
end sub private sub closers()
if rs.state <> adstateclose then
rs.close()
set rs = nothing
end if
end sub end class
%>