2017-04-26 39 views
0

我正嘗試使用VBA連接到Netezza。我啓用了以下內容:如何通過VBA連接到Netezza(用於分析的PureData系統)

  • 的Microsoft Excel 15.0對象庫
  • 的Microsoft Office 15.0對象庫
  • Microsoft ActiveX數據對象6.1庫
  • Visual Basic應用程序

這裏是我的代碼:

Sub NZConn() 

Dim cmd As New ADODB.Command 
Dim rs As New ADODB.Recordset 
Dim x As Variant 

Set cmd = New ADODB.Command 
Set RS = New ADODB.Recordset 

cmd.ActiveConnection = "Driver={Netezza " & _ 
"ODBC};servername=servername;port=####;database=database;" & _ 
"username=username;password=password;" 

cmd.ActiveConnection.CursorLocation = adUseClient 
cmd.CommandTimeout = 120 
cmd.CommandType = adCmdText 

x = "Write Query here" 
cmd.CommandText = x 

Set rs = cmd.Execute 
Sheet1.Range("A1").CopyFromRecordset rs 

cmd.ActiveConnection.Close 

End Sub 

我可以讓代碼在沒有返回錯誤的情況下運行,但是沒有任何東西是從記錄集粘貼的,這導致我認爲這可能與連接字符串的結構有關。

我有服務器,用戶名,密碼,數據庫,端口和驅動程序。

我需要先建立/打開ActiveConnection嗎?

+0

嘗試'服務器名=服務器名,端口號' – Tehscript

+0

從聲明中減去'新'作爲'Dim cmd As ADODB.Command'和'Dim rs As ADODB.Recordset'。並確保rs是小寫字母,作爲'Set rs = New ADODB.Recordset'。 – Tehscript

+0

Driver = {NetezzaSQL}; servername = myServerAddress; port = myPortNumber; database = myDataBase; username = myUsername; password = myPassword –

回答

1

我能搞清楚這個問題我自己。我發現Aginity的'工具'選項卡中有一個命令行構建器,它幫助指定我連接到Netezza所需的確切連接字符串。一旦我有這個連接字符串,我得到了'架構不匹配'錯誤。在下載Netezza的32位ODBC驅動程序後,該方法運行良好。這裏是下面的更新代碼:

Dim cn As ADODB.Connection 
Dim rs As ADODB.Recordset   
Dim iCols As Integer    
Dim DB As String, User As String, PW As String, ConnectionString As String 
Dim Server As String, Query As String 
Dim SQLTable As Worksheet 

Set cn = New ADODB.Connection 
Set rs = New ADODB.Recordset 
Set SQLTable = Sheet1 

Server = SQLTable.Cells(2,3).Value 
User = SQLTable.Cells(2,4).Value 
PW = SQLTable.Cells(2,5).Value 
DB = SQLTable.Cells(2,6).Value 
Query = SQLTable.Cells(2,7).Value 

ConnectionString = "Driver={NetezzaSQL};" & _ 
        "server=" & Server & ";" & _ 
        "UserName=" & User & ";" & _ 
        "Password=" & PW & ";" & _ 
        "Database=" & DB & ";" & _ 
        "Query Timeout=120" 

cn.Open (ConnectionString) 
rs.Open (Query), cn 

For iCols = 0 To RS.Fields.count - 1 
    Worksheets("Sheet2").Cells(1, iCols + 1).Value = rs.Fields(iCols).Name 
Next 
Worksheets("Sheet2").Cells(2, "A").CopyFromRecordset rs 

rs.Close 
cn.Close 

NB:

  • 「IBM NETEZZA ODBC驅動程序 - 32位」 是我下載什麼
  • 「ODBC-DRIVER-FOR-NETEZZA-7-X86 「是什麼出現在我的軟件中心安裝
  • 」名稱:NetezzaSQL;版本:7.00.04.41188;公司:www.ibm.com;文件:NSQLODBC.DLL「是現在在我的32位' ODBC數據源管理員「窗口
1

我認爲你的連接字符串是好的,是的你應該首先打開一個連接。

像這樣:

AccessConnect = "Driver={Netezza " & _ 
"ODBC};servername=servername;port=####;database=database;" & _ 
"username=username;password=password;" 

Dim Conn1 As New adodb.Connection 
Conn1.ConnectionString = AccessConnect 
Conn1.Open 

那麼這將是

Set RS = Conn1.Execute(x) 'where x is your query 
+0

我一直在繼續解決此問題,但尚未找到工作像我對Oracle和Microsoft SQL Server的產品。 –

相關問題