一个ACCESS数据库访问的类第13页

2018-09-06 10:30

阅读:778

  大部分ASP应用,都离不开对数据库的访问及操作,所以,对于数据库部分的访问操作,我们应该单独抽象出来,封装成一个单独的类。如果所用语言支持继承,可以封装一个这样的类,然后在数据操作层继承即可。下面是我写的一个ACCESS数据库访问的类,针对ACCESS作了优化,不过因为缺少足够的应用测试,可能仍然存在未知的bug及应用限制,主要代码如下:
<%
ClassOledbPrivateIDataPath
PrivateIConnectionStringPrivateConn
PrivateCmd
PrivateParam
PrivateRsPublicPropertyLetDataPath(ByValValue)
IDataPath=Value
IConnectionString=Provider=Microsoft.Jet.OLEDB.4.0;DataSource=&Server.MapPath(IDataPath)
EndPropertyPublicPropertyGetDataPath()
DataPath=IDataPath
EndPropertyPublicPropertyLetConnectionString(ByValValue)
IConnectionString=Value
EndPropertyPublicPropertyGetConnectionString()
ConnectionString=IConnectionString
EndPropertyPublicFunctionOpenConn()
IfConn.State=adStateClosedThen
Conn.OpenConnectionString
EndIf
SetOpenConn=Conn
EndFunctionPublicFunctionInsert(ByValSql,ByValValues)
OpenConn()
Rs.OpenSql,Conn,3,3,adCmdText
Rs.AddNew
Dimi,l
l=UBound(Values)
Fori=1Tol+1
Rs(i)=Values(i-1)
Next
Rs.Update
Insert=Rs(0)
EndFunctionPublicFunctionExecute(ByValSql)
OpenConn()
SetExecute=Conn.Execute(Sql)
EndFunctionPublicFunctionExecuteScalar(ByValSql)
DimiRs:SetiRs=Execute(Sql)
IfNotiRs.BOFThenExecuteScalar=iRs(0)
EndFunctionPublicFunctionExecuteNonQuery(ByValSql)
OpenConn()
CallConn.Execute(Sql,ExecuteNonQuery)
EndFunctionPublicFunctionInsertSp(ByValSql,ByValParams)
OpenConn()
Rs.OpenSql,Conn,3,3,adCmdStoredProc
Rs.AddNew
Dimi,l
l=UBound(Params)
Fori=1Tol+1
Rs(i)=Params(i-1)
Next
Rs.Update
InsertSp=Rs(0)
EndFunctionPublicFunctionExecuteSp(ByValSpName,ByValParams)
WithCmd
Set.ActiveConnection=OpenConn()
.Prepared=True
SetExecuteSp=.Execute(,Params)
EndWith
EndFunctionPublicFunctionExecuteDataTableSp(ByValSpName,ByValParams)
OpenConn()
IfRs.State<>adStateCloseThen
Rs.Close()
EndIf
DimSpStr
IfIsNull(Params)OrIsEmpty(Params)Then
SpStr=SpName
Else
IfIsArray(Params)Then
SpStr=Execute&SpName&&Join(Params,,)
Else
SpStr=Execute&SpName&&Params
EndIf
EndIf
CallRs.Open(SpStr,Conn,1,1,adCmdStoredProc)
SetExecuteDataTableSp=Rs
EndFunctionPublicFunctionExecuteScalarSp(ByValSpName,ByValParams)
DimiRs:SetiRs=ExecuteSp(SpName,Params)
IfNotiRs.BOFThenExecuteScalarSp=iRs(0)
EndFunctionPublicFunctionExecuteNonQuerySp(ByValSpName,ByValParams)
WithCmd
Set.ActiveConnection=OpenConn()
.CommandText=SpName
.CommandType=&H0004
.Prepared=True
Call.Execute(ExecuteNonQuerySp,Params)
EndWith
EndFunctionPrivateSubClass_Initialize()
SetConn=Server.CreateObject(ADODB.Connection)
SetCmd=Server.CreateObject(ADODB.Command)
SetParam=Server.CreateObject(ADODB.Parameter)
SetRs=Server.CreateObject(ADODB.RecordSet)
DataPath=/data/data.mdb这里写你的数据库默认路径,建议更改名称及扩展名
EndSub
PrivateSubClass_Terminate()
SetParam=Nothing
SetCmd=Nothing
CloseRs()
CloseConn()
EndSubPrivateSubCloseConn()
IfConn.State<>adStateCloseThen
Conn.Close()
SetConn=Nothing
EndIf
EndSubPrivateSubCloseRs()
IfRs.State<>adStateCloseThen
Rs.Close()
SetRs=Nothing
EndIf
EndSubEndClass
%>
123下一页阅读全文


评论


亲,登录后才可以留言!