1.设置引用

image.png
image.png

2.连接模块代码编写

  1. Option Explicit
  2. 'IP
  3. Public Const strIP As String = "172.20.2.49"
  4. '用户名
  5. Public Const strUser As String = "admin"
  6. '密码
  7. Public Const strPasswd As String = "TREchina0"
  8. '数据库名称
  9. Public Const strDB As String = "StorePL"
  10. Public strSQL As String
  11. Public rst As New ADODB.Recordset
  12. Public Conn As New ADODB.Connection
  13. Public Sub cnOpen()
  14. On Error GoTo ERR_ConDwh
  15. If Conn Is Nothing Then
  16. Set Conn = New ADODB.Connection
  17. End If
  18. If Conn.State = 0 Then
  19. With Conn
  20. .Provider = "SQLOLEDB.1"
  21. .ConnectionString = "Persist Security Info=True;" & _
  22. "Data Source=" & strIP & ";" & _
  23. "Initial Catalog=" & strDB & ";" & _
  24. "User ID=" & strUser & ";" & _
  25. "Password=" & strPasswd
  26. .ConnectionTimeout = 10
  27. .CommandTimeout = 0
  28. .CursorLocation = adUseClient
  29. .Open
  30. End With
  31. End If
  32. Exit Sub
  33. ERR_ConDwh:
  34. MsgBox "无法接入数据库"
  35. End
  36. End Sub
  37. Public Sub cnClose()
  38. Conn.Close
  39. End Sub

3.使用

                Private Sub UpdateButton_Click()
        ModDBConnect.cnOpen

        strSQL = "exec F_STORE_DataImport '" & Range("C" & r).Value & "','" & FilePath & "'"

        rst.Open strSQL, Conn

        strSQL = "exec F_STORE_DataDistribute '" & Range("C" & r).Value & "'"

        rst.Open strSQL, Conn

        ModDBConnect.cnClose
        End Sub

BCP storepl.dbo.BizDocDetail_T in ‘+ ‘\172.20.1.43\青島共通\002.業務関係\PL_TEST’ + ‘\bizdata\’ + ‘BizDocData‘+CONVERT(varchar(10),@CmdDate,112)+’.tsv’ + ‘ -S172.20.2.49 -Uadmin -PTREchina0 -t \t -c -F2’

Range(“A2”).CopyFromRecordset rst