vb连接sql模块代码
2024-07-21 02:24:53
供稿:网友
public conn as new adodb.connection
public rs as new adodb.recordset
public addflag as boolean
public function opencn(byval cip as string) 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 = cip '服务器的名字
conn.properties("initial catalog").value = "zqoa" '库名
'conn.properties("integrated security").value = "sspi" '登陆类型
conn.properties("user id").value = "sa"
conn.properties("password").value = "sa"
conn.open
opencn = true
addflag = true
exit function
strerrmag:
mag = "data can't connect"
call msgbox(mag, vbokonly, "error:data connect")
addflag = false
exit function
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
end
'exit function
strerrmag:
mag = "data not connect"
call msgbox(mag, vbokonly, "error:connect")
openrs = false
end
'exit function
end function
public sub clors()
on error resume next
if rs.state <> adstateclosed then rs.clone
set rs = nothing
end sub