Function conn() As BooleanDim adoconn As adocb.ConnectionSet adoconn = New ADODB.Connectiont1 = GetFromIni("database", "server", ".\\thjl.ini")Debug.Print t1t2 = GetFromIni("database", "user", ".\\thjl.ini")t3 = GetFromIni("database", "password", ".\\thjl.ini")t4 = GetFromIni("database", "data", ".\\thjl.ini")constr = "Provider=SQLOLEDB.1;Persist Security Info=True;Initial Catalog=" & t4 & ";Data Source=" & t1 & ""adoconn.ConnectionString = constradoconn.CursorLocation = adUseClientadoconn.Open constr, t2, t3End Function我定义的函数,VB连接sql,我定义的是不是有问题,该怎么调用呢
2 回答
杨魅力
TA贡献1811条经验 获得超6个赞
修改一下你的代码
改成
function conn() As adodb.connection
Dim adoconn As adocb.Connection
Set adoconn = New ADODB.Connection
t1 = GetFromIni("database", "server", ".\\thjl.ini")
Debug.Print t1
t2 = GetFromIni("database", "user", ".\\thjl.ini")
t3 = GetFromIni("database", "password", ".\\thjl.ini")
t4 = GetFromIni("database", "data", ".\\thjl.ini")
constr = "Provider=SQLOLEDB.1;Persist Security Info=True;Initial Catalog=" & t4 & ";Data Source=" & t1 & ""
adoconn.ConnectionString = constr
adoconn.CursorLocation = adUseClient
adoconn.Open constr, t2, t3
conn = adoconn
End function
这样你可以在使用时如下
rs.open sql,conn
当然,你的代码还得改进,比如错误处理之类的.还有
你也可以做成 property ,这样用起来更形象
添加回答
举报
0/150
提交
取消