无法使用 VBA 代码从 excel 连接到 Teradata - 无法通过网络访问 Teradata 服务器
Unable to connect to Teradata from excel using VBA code - Teradata Server can't be reached over the network
我一直在尝试使用 vba 代码从 Excel 连接到 Teradata,但出现以下错误:
无法通过网络访问 Teradata 服务器。我已经能够从 Teradata SQL 助手成功连接并且我也成功地 ping 了 Teradata 服务器(我在我的笔记本电脑上使用 Teradata Express 版本,它运行在 VMware 播放器上)。我还通过 Excel 数据连接向导和 Microsoft 查询成功连接到 Teradata。我还关闭了防火墙并检查我是否能够连接但仍然无法连接。请让我知道哪里出错了。
请查找以下代码:
Private Sub CommandButton1_Click()
Dim conn As ADODB.Connection
Dim rec1 As ADODB.Recordset
Dim thisSql As String
Set conn = New ADODB.Connection
conn.Open "Driver=Teradata;DBCName=dsnname;Databasename=dbname;Uid=Userid;Pwd=****;"
thisSql = "sel * from customer_db.customer"
Set rec1 = New ADODB.Recordset
rec1.Open thisSql, conn
With Sheet1.QueryTables.Add(Connection:=rec1, Destination:=Sheet2.Range("A1"))
.Name = "data"
.FieldNames = True
.Refresh BackgroundQuery:=False
End With
End Sub
如果更改 Connection 对象的 Open 方法会怎样:
conn.Open "DSN=dsnname;Databasename=dbname;Uid=Userid;Pwd=****;"
这个连接脚本对我有用。
' Add Microsoft ActiveX Data Objects 2.8 Library in References
' When installing Teradata SQL Assistant, include the ODBC Driver for Teradata will install the TDOLEDB provider
' This example connects to Teradata, deletes the contents of MyTable & inserts row 7- 8 from the active spreadsheet
Function OpenConn() As Object
Set OpenConn = New ADODB.Connection
Dim myConnectionString As String
myConnectionString = "Provider=TDOLEDB;Data Source=MyTeradataServerName;Persist Security Info=True;User ID=MyTeradataUserID;Password=MyTeradataPass;Session Mode=ANSI;DefaultDatabase=GRP_BCE_FINANCE_IM;MaxResponseSize=65477;"
OpenConn.Open myConnectionString
End Function
Sub CloseConn(conn As Object)
conn.Close
Set conn = Nothing
End Sub
Sub PushCCHier()
Dim TeraObjCmd As New ADODB.Command
Dim TeraObjRs As ADODB.Recordset
Dim TeraObjRs2 As ADODB.Recordset
Dim TeraCnxn As Object
Set TeraCnxn = OpenConn()
TeraObjCmd.ActiveConnection = TeraCnxn
'Clear Previous Data
TeraObjCmd.ActiveConnection = TeraCnxn
TeraObjCmd.CommandText = "delete from MyTable"
TeraObjCmd.Execute
'Load New Data
Set TeraObjRs2 = New ADODB.Recordset
TeraObjRs2.Open "SELECT * FROM MyTable where 1 = 2 ", TeraCnxn, adOpenStatic, adLockOptimistic
With TeraObjRs2
For irow = 7 To 8 'loading results from rows in my spredsheet
If Len(Trim(Range("B" & irow).Value)) <> 0 Then 'Avoid blank rows
.AddNew
.Fields(0) = Range("B" & irow).Value
End If
Next
.UpdateBatch
.Close
End With
' clean up objects
Set objCmd = Nothing
Call CloseConn(TeraCnxn)
MsgBox "Update Complete!"
End Sub
用于连接到 Teradata 的备用连接字符串:
"Provider=MSDASQL.1;Password=myPassword;Persist Security Info=False;User ID=myUserid;Data Source=myDSN"
我一直在尝试使用 vba 代码从 Excel 连接到 Teradata,但出现以下错误: 无法通过网络访问 Teradata 服务器。我已经能够从 Teradata SQL 助手成功连接并且我也成功地 ping 了 Teradata 服务器(我在我的笔记本电脑上使用 Teradata Express 版本,它运行在 VMware 播放器上)。我还通过 Excel 数据连接向导和 Microsoft 查询成功连接到 Teradata。我还关闭了防火墙并检查我是否能够连接但仍然无法连接。请让我知道哪里出错了。
请查找以下代码:
Private Sub CommandButton1_Click()
Dim conn As ADODB.Connection
Dim rec1 As ADODB.Recordset
Dim thisSql As String
Set conn = New ADODB.Connection
conn.Open "Driver=Teradata;DBCName=dsnname;Databasename=dbname;Uid=Userid;Pwd=****;"
thisSql = "sel * from customer_db.customer"
Set rec1 = New ADODB.Recordset
rec1.Open thisSql, conn
With Sheet1.QueryTables.Add(Connection:=rec1, Destination:=Sheet2.Range("A1"))
.Name = "data"
.FieldNames = True
.Refresh BackgroundQuery:=False
End With
End Sub
如果更改 Connection 对象的 Open 方法会怎样:
conn.Open "DSN=dsnname;Databasename=dbname;Uid=Userid;Pwd=****;"
这个连接脚本对我有用。
' Add Microsoft ActiveX Data Objects 2.8 Library in References
' When installing Teradata SQL Assistant, include the ODBC Driver for Teradata will install the TDOLEDB provider
' This example connects to Teradata, deletes the contents of MyTable & inserts row 7- 8 from the active spreadsheet
Function OpenConn() As Object
Set OpenConn = New ADODB.Connection
Dim myConnectionString As String
myConnectionString = "Provider=TDOLEDB;Data Source=MyTeradataServerName;Persist Security Info=True;User ID=MyTeradataUserID;Password=MyTeradataPass;Session Mode=ANSI;DefaultDatabase=GRP_BCE_FINANCE_IM;MaxResponseSize=65477;"
OpenConn.Open myConnectionString
End Function
Sub CloseConn(conn As Object)
conn.Close
Set conn = Nothing
End Sub
Sub PushCCHier()
Dim TeraObjCmd As New ADODB.Command
Dim TeraObjRs As ADODB.Recordset
Dim TeraObjRs2 As ADODB.Recordset
Dim TeraCnxn As Object
Set TeraCnxn = OpenConn()
TeraObjCmd.ActiveConnection = TeraCnxn
'Clear Previous Data
TeraObjCmd.ActiveConnection = TeraCnxn
TeraObjCmd.CommandText = "delete from MyTable"
TeraObjCmd.Execute
'Load New Data
Set TeraObjRs2 = New ADODB.Recordset
TeraObjRs2.Open "SELECT * FROM MyTable where 1 = 2 ", TeraCnxn, adOpenStatic, adLockOptimistic
With TeraObjRs2
For irow = 7 To 8 'loading results from rows in my spredsheet
If Len(Trim(Range("B" & irow).Value)) <> 0 Then 'Avoid blank rows
.AddNew
.Fields(0) = Range("B" & irow).Value
End If
Next
.UpdateBatch
.Close
End With
' clean up objects
Set objCmd = Nothing
Call CloseConn(TeraCnxn)
MsgBox "Update Complete!"
End Sub
用于连接到 Teradata 的备用连接字符串:
"Provider=MSDASQL.1;Password=myPassword;Persist Security Info=False;User ID=myUserid;Data Source=myDSN"