请问我们可以通过Excel VBA建立ADO或与Redshift的任何连接吗? 到目前为止,我们已连接到SQL Server并获得了如下的工作结果;
-SQL Server 2012/2016的当前代码
Sub Get_DB_Conncetion(StrDBName As String, StrSQL As String, DestSht As String, DestCell As String)
On Error GoTo Err_DBConnectOpen
Dim con As New ADODB.Connection
Dim conStr As String
Dim rs As Variant
Dim cmd As Object
'Connect String
conStr = "Provider=Sqloledb;Data Source=" & StrSvr _
& ";Initial Catalog=" & StrDBName _
& ";Connect Timeout=10" _
& ";user id=" & StrID _
& ";password=" & StrPWD _
& ""
Debug.Print Now & ":Connect String:" & conStr
'Connect to Database
con.Open conStr
'Execute SQL
con.CommandTimeout = 900
Set rs = con.Execute(StrSQL)
If Not rs.EOF Then
With Sheets(DestSht)
.Activate
.Range(DestCell).CopyFromRecordset rs
End With
End If
'Close Session
con.Close
Set con = Nothing
Exit Sub
'Error Handling
Err_DBConnectOpen:
MsgBox "Can't Connect" & vbCrLf & _
Err.Description, vbOKOnly, vbCritical
EXIT_FLG = True
'Close Connection
If con.State <> ADODB.adStateClosed Then
con.Close
End If
Set con = Nothing
Exit Sub
End Sub
直到我们可以确认一切正常,直到我们需要每天检查和评估SQL Server和Redshift的数字。为了提高效率和准确性,我们希望抛出检查查询并尽可能自动地获取结果。
任何建议将再次受到赞赏。 谢谢。