加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

精简VB6连接SQL数据库的语句

发布时间:2020-12-16 23:26:28 所属栏目:大数据 来源:网络整理
导读:在vb的数据应用开发中,少不了连接SQL数据库和打开数据表的操作,在需要连接打开数据表窗口中都要到如下语句, Dim objcn as connection Dim objre as recordset Set objcn = New Connection With objcn .Open "DSN=" gCurrentServer ";Description=" gCurre


在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)

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读