[英]Using ADO in VBA to connect to PostgreSQL
我無法找到使用 VBA ADO 從 Excel 連接到 PostgreSQL 數據庫的清晰可靠示例。 誠然,我是 VBA 新手,大多數示例和教程都非常以 Access 或 MSSQL 為中心。 (我主要使用 Ruby、Rails、Perl 和 PostgreSQL。)
我正在尋找代碼來連接並返回一個簡單的查詢 (SELECT * FROM customers;) 到 Excel 工作表。 連接參數(服務器 ip、用戶、密碼、數據庫)位於單獨工作表中的單元格內。
感謝您的幫助和耐心。
代碼:
Sub ConnectDatabaseTest()
Dim cnn As ADODB.connection
Dim cmd As ADODB.Command
Dim param As ADODB.Parameter
Dim xlSheet As Worksheet
Dim rs As ADODB.Recordset
Dim sConnString As String
Dim i As Integer
' Connection Parameters
Dim strUsername As String
Dim strPassword As String
Dim strServerAddress As String
Dim strDatabase As String
' User:
strUsername = Sheets("CONFIG").Range("B4").Value
' Password:
strPassword = Sheets("CONFIG").Range("B5").Value
' Server Address:
strServerAddress = Sheets("CONFIG").Range("B6").Value
' Database
strDatabase = Sheets("CONFIG").Range("B3").Value
Set xlSheet = Sheets("TEST")
xlSheet.Activate
Range("A3").Activate
Selection.CurrentRegion.Select
Selection.ClearContents
Range("A1").Select
Set cnn = New ADODB.connection
sConnString = "DRIVER={PostgreSQL Unicode};DATABASE=" & strDatabase & ";SERVER=" & strServerAddress & _
";UID=" & strUsername & ";PWD=" & strPassword
cnn.Open sConnString
cmd.ActiveConnection = cnn
Dim strSQL As String
strSQL = "SELECT * FROM customers"
cmd.CommandType = ADODB.CommandTypeEnum.adCmdText
cmd.ActiveConnection = cnn
cmd.CommandText = strSQL
...
它似乎在這里中斷:cmd.ActiveConnection = cnn
編輯:添加了示例代碼。
編輯:sConnString 設置為:
DRIVER={PostgreSQL35W};DATABASE=my_database;SERVER=1.2.3.4;UID=analyst;PWD=sekrit
更新 2/7:我更改了連接字符串中的“DRIVER”參數:
sConnString = "DRIVER={PostgreSQL Unicode};DATABASE=" & strDatabase & ";SERVER=" & strServerAddress & _
";UID=" & strUsername & ";PWD=" & strPassword & ";"
...我得到一個不同的錯誤:'運行時錯誤 91:對象變量或塊變量未設置'
嗯。 想法?
我不想使用 DSN,因為我使用的是 ODBC 驅動程序而不是 OLE DB。 通過引用 DSN,上面的代碼只需很少的更改即可工作。
請參閱此問題,了解一旦我開始懷疑 OLE DB/ODBC 的問題,我是如何找到答案的。 ADO 是與 ODBC 驅動程序一起工作還是僅與 OLE DB 提供程序一起工作?
新代碼在這里:
Sub GetCustomers()
Dim oConn As New ADODB.connection
Dim cmd As New ADODB.Command
' Connection Parameters
Dim strUsername As String
Dim strPassword As String
Dim strServerAddress As String
Dim strDatabase As String
' User:
strUsername = Sheets("CONFIG").Range("B4").Value
' Password:
strPassword = Sheets("CONFIG").Range("B5").Value
' Server Address:
strServerAddress = Sheets("CONFIG").Range("B6").Value
' Database
strDatabase = Sheets("CONFIG").Range("B3").Value
oConn.Open "DSN=my_system_dsn;" & _
"Database=" & strDatabase & ";" & _
"Uid=" & strUsername & ";" & _
"Pwd=" & strPassword
Set xlSheet = Sheets("CUSTOMERS")
xlSheet.Activate
Range("A3").Activate
Selection.CurrentRegion.Select
Selection.ClearContents
Range("A1").Select
Dim strSQL As String
strSQL = "SELECT * FROM customers"
cmd.CommandType = ADODB.CommandTypeEnum.adCmdText
cmd.ActiveConnection = oConn
cmd.CommandText = strSQL
Set rs = New ADODB.Recordset
Set rs = cmd.Execute
For i = 1 To rs.Fields.Count
ActiveSheet.Cells(3, i).Value = rs.Fields(i - 1).Name
Next i
xlSheet.Range(xlSheet.Cells(3, 1), _
xlSheet.Cells(3, rs.Fields.Count)).Font.Bold = True
ActiveSheet.Range("A4").CopyFromRecordset rs
xlSheet.Select
Range("A3").Select
Selection.CurrentRegion.Select
Selection.Columns.AutoFit
Range("A1").Select
rs.Close
oConn.Close
Set cmd = Nothing
Set param = Nothing
Set rs = Nothing
Set cnn = Nothing
Set xlSheet = Nothing
End Sub
系統 DSN 配置為使用 PostgreSQL Unicode 驅動程序。 即使有可用的提供程序,我也選擇不使用 OLE DB。 如果你查看PGFoundry,你會發現它有很多問題,而且幾年沒有更新。
在原始代碼中,“PostgreSQL35W”是一個包含默認主機和端口的 DSN 名稱。 當您更改為“PostgreSQL Unicode”時,它是一個驅動程序並且您的連接字符串缺少端口值。 記住直接從驅動訪問PostgreSQL,至少需要5個參數:
如果您使用的是 DSN,某些參數可能會被定義為默認值。
不確定實際數據庫連接的詳細信息,但是您的語句有一個簡單但常見的錯誤:在處理對象時需要使用“set”:
set cmd.ActiveConnection = cnn
設置 cmd = 新建 ADODB.Command cmd.ActiveConnection = cnn
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.