我一直在尝试使用vba代码从Excel连接到Teradata,但我收到以下提到的错误: 无法通过网络联系Teradata Server。我已经能够成功连接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
答案 0 :(得分:2)
如果更改Connection对象的Open方法,该怎么办:
conn.Open "DSN=dsnname;Databasename=dbname;Uid=Userid;Pwd=****;"
答案 1 :(得分:1)
此连接脚本对我有用。
'在参考中添加Microsoft ActiveX Data Objects 2.8库 '安装Teradata SQL Assistant时,包含用于Teradata的ODBC驱动程序将安装TDOLEDB提供程序 '此示例连接到Teradata,删除MyTable&的内容。从活动电子表格中插入第7-8行
函数OpenConn()As Object 设置OpenConn = New ADODB.Connection 将myConnectionString调暗为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 结束功能
Sub CloseConn(conn As Object) conn.Close 设置conn = Nothing 结束子
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
答案 2 :(得分:0)
连接到 Teradata 的替代连接字符串:
students