VB中数据库的常用连接方式

Option Explicit

Public Function ExecuteSQL(ByVal sql As String) As ADODB.Recordset                        'whd_project.dsn
Dim connectstring As String
Dim cnn As ADODB.Connection
Dim rst As ADODB.Recordset
Dim sTokens() As String
On Error GoTo executeSQL_Error
connectstring = "FileDSN=whd_project.dsn;uid=sa;pwd="
Set cnn = New ADODB.Connection
cnn.Open connectstring
sTokens = Split(sql)
If InStr("INSERT,DELETE,UPDATE,EXECUTE", VBA.UCase$(sTokens(0))) Then
cnn.Execute (sql)
Else
Set rst = New ADODB.Recordset
rst.Open VBA.Trim$(sql), cnn, adOpenKeyset, adLockOptimistic
Set ExecuteSQL = rst
End If
executeSQL_Error:
Exit Function
End Function


Public Function ExecuteSQLGld(ByVal sql As String) As ADODB.Recordset                     'whd_gld.dsn
Dim connectstring As String
Dim cnn As ADODB.Connection
Dim rst As ADODB.Recordset
On Error GoTo executeSQL_Error
connectstring = "FileDSN=gld.dsn;uid=sa;pwd="
Set cnn = New ADODB.Connection
cnn.Open connectstring
cnn.Execute (sql)
Set rst = New ADODB.Recordset
rst.Open VBA.Trim$(sql), cnn, adOpenKeyset, adLockOptimistic
Set ExecuteSQLGld = rst

executeSQL_Error:

Exit Function
End Function


Public Sub ExecuteINsSQLGld(ByVal sql As String)                                           'whd_gld.dsn(礚)
Dim connectstring As String
Dim cnn As ADODB.Connection

On Error GoTo executeSQL_Error
connectstring = "FileDSN=gld.dsn;uid=sa;pwd="
Set cnn = New ADODB.Connection
cnn.Open connectstring
cnn.Execute (sql)

executeSQL_Error:

Exit Sub
End Sub


Public Function ExecuteSQLBom(ByVal sql As String) As ADODB.Recordset                       'whd_bomtab.dsn
Dim connectstring As String
Dim cnn As ADODB.Connection
Dim rst As ADODB.Recordset
Dim sTokens() As String
'On Error GoTo executeSQL_Error
connectstring = "FileDSN=whd_bomtab.dsn;uid=sa;pwd="
Set cnn = New ADODB.Connection
cnn.Open connectstring
cnn.Execute (sql)
Set rst = New ADODB.Recordset
rst.Open VBA.Trim$(sql), cnn, adOpenKeyset, adLockOptimistic
Set ExecuteSQLBom = rst

executeSQL_Error:
Exit Function
End Function


Public Function ExecuteSQLb(ByVal sql As String) As ADODB.Recordset                          '场更家遏(whd_bomtab)
Dim connectstring As String
Dim cnn As ADODB.Connection
Dim rst As ADODB.Recordset
Dim sTokens() As String
On Error GoTo executeSQL_Error
connectstring = "FileDSN=whd_bomtab.dsn;uid=sa;pwd="
Set cnn = New ADODB.Connection
cnn.Open connectstring
sTokens = Split(sql)
If InStr("INSERT,DELETE,UPDATE,EXECUTE", VBA.UCase$(sTokens(0))) Then
cnn.Execute (sql)
Else
Set rst = New ADODB.Recordset
rst.Open Trim$(sql), cnn, adOpenKeyset, adLockOptimistic
Set ExecuteSQLb = rst
End If
executeSQL_Error:
Exit Function
End Function


Public Function ExecuteSQLBaoZh(ByVal sql As String) As ADODB.Recordset                         'whd_baozh.dsn
Dim connectstring As String
Dim cnn As ADODB.Connection
Dim rst As ADODB.Recordset
'On Error GoTo executeSQL_Error
connectstring = "FileDSN=whd_baozh.dsn;uid=sa;pwd="
Set cnn = New ADODB.Connection
cnn.Open connectstring
cnn.Execute (sql)
Set rst = New ADODB.Recordset
rst.Open VBA.Trim$(sql), cnn, adOpenKeyset, adLockOptimistic
Set ExecuteSQLBaoZh = rst

executeSQL_Error:

Exit Function
End Function


Public Function existVal(ByVal table As String, ByVal ziduan As String) As Boolean
 Dim sql As String
 Dim rst As ADODB.Recordset
 sql = "select *  from " & table & " where xm='" & ziduan & "'"
 Set rst = ExecuteSQLGld(sql)
 If rst.RecordCount = 0 Then
 existVal = False
 Else
 existVal = True
 End If
 End Function

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值