三层架构之数据库访问层(VB)
2024-07-21 02:20:43
供稿:网友
'########数据库访问层##########
'返回一个adocn连接对象
'与c#不同,这里不能关闭连接及释放内存
public function getcn(sdbpath as string) as adodb.connection
const spro as string = "provider=microsoft.jet.oledb.4.0;data source="
const sdbpwd as string = ";jet oledb:database password=qq:48403849"
dim sdbpath as string
dim m_cn as adodb.connection
set m_cn = new adodb.connection
m_cn.cursorlocation = aduseclient '客户端游标
if m_cn.state <> adstateclosed then m_cn.close
on error goto conerr
m_cn.open spro & sdbpath & sdbpwd
set getcn = m_cn
exit function
conerr:
set getcn = nothing
msgbox "数据库连接错误", vbcritical
end function
'执行一句sql语句,正确返回 1
public function excutesql(ssql as string) as integer
dim m_cn as new adodb.connection
on error goto err
set m_cn = getcn(sg_dbpath)
m_cn.execute ssql
m_cn.close
set m_cn = nothing
excutesql = 1
exit function
err:
excutesql = 0
set m_cn = nothing
end function
'执行一组sql语句,正确返回1
public function excutesqlex(ssql() as string) as integer
'调用事务处理
dim m_cn as new adodb.connection, i as integer
if ubound(ssql) < 0 then exit function
on error goto err
set m_cn = getcn(sg_dbpath)
m_cn.begintrans
for i = 0 to ubound(ssql) - 1
m_cn.execute ssql(i)
next i
m_cn.committrans
m_cn.close
set m_cn = nothing
excutesqlex = 1
exit function
err:
excutesqlex = 0
m_cn.rollbacktrans
m_cn = nothing
end function
'未完,待补充