VB连接SQL数据库的模块
2024-07-21 02:21:05
供稿:网友
public conn as new adodb.connection
public rs as new adodb.recordset
public addflag as boolean
public function opencn() as boolean
dim mag as string
on error goto strerrmag
set conn = new adodb.connection
conn.connectiontimeout = 25
conn.provider = "sqloledb"
conn.properties("data source").value = "127.0.0.1" '服务器的名字
conn.properties("initial catalog").value = "chart" '库名
conn.properties("integrated security").value = "sspi" '登陆类型
conn.open
opencn = true
addflag = true
exit function
strerrmag:
mag = "data can't connect"
call msgbox(mag, vbokcancel, "error:data connect")
addflag = false
end
end function
public sub clocn()
on error resume next
if conn.state <> adstateclosed then conn.close
set conn = nothing
end sub
public function openrs(byval strsql as string) as boolean '连接数据库记录集
dim mag as string
dim rpy as boolean
on error goto strerrmag
set rs = new adodb.recordset
if addflag = false then rpy = true
with rs
.activeconnection = conn
.cursorlocation = aduseclient
.cursortype = adopenkeyset
.locktype = adlockoptimistic
.open strsql
end with
addflag = true
openrs = true
exit function
strerrmag:
mag = "data not connect"
call msgbox(mag, vbokcancel, "error:connect")
openrs = false
end
end function
public sub clors()
on error resume next
if rs.state <> adstateclosed then rs.clone
set rs = nothing
end sub