在vb的数据应用开发中,少不了连接sql数据库和打开数据表的操作,在需要连接打开数据表窗口中都要到如下语句,
Dim objcn as connection
Dim objre as recordset
Set objcn = New Connection
With objcn
.Open "DSN=" & gCurrentServer & ";Description=" & gCurrentServer & ";SERVER=" & gCurrentServer & "/sql2000;" & _
"UID=sa;PWD=123;WSID=JJB;" & _
"DATABASE=MysqL;" & _
"Address=//" & gCurrentServer & "/pipe/MSsql$sql2000/sql/query"
End With
Set objre = New Recordset
With objre
.CursorLocation = adUseClient
.CursorType = adOpenStatic
.LockType = adLockOptimistic
End With
.....
为了精简,在模块中建立如下两个过程。
Public Sub initCN(ByRef cn As Connection)
Set cn = New Connection
With cn
.Open "DSN=" & gCurrentServer & ";Description=" & gCurrentServer & ";SERVER=" & gCurrentServer & "/sql2000;" & _
"UID=sa;PWD=123;WSID=JJB;" & _
"DATABASE=MysqL;" & _
"Address=//" & gCurrentServer & "/pipe/MSsql$sql2000/sql/query"
End With
End Sub
Public Sub initRe(ByRef re As Recordset)
Set re = New Recordset
With re
.CursorLocation = adUseClient
.CursorType = adOpenStatic
.LockType = adLockOptimistic
End With
End Sub
这样,上述的冗长语句可用下述精简的语句代替,15行变成4行
dim objcn as connecttiondim objcn as recordsetCall initCN(objcn)Call initRe(objre)
原文链接:https://www.f2er.com/vb/262857.html