无法通过VBA宏连接到Teradata,机制为LDAP

时间:2017-12-05 13:54:18

标签: vba excel-vba teradata excel

尝试使用以下代码连接到Teradata并且工作正常

  Worksheets("BO").range("A:C").Clearcontents
  conn.Open "Driver={Teradata};" & _
  "DBCName=" & TDServer & ";" & _
  "Database=" & TDDb & ";" & _
  "Uid=" & TDUname & ";" & _
  "Pwd=" & TDPword & ""

无法连接到" LDAP"机制和投掷用户名/密码即使在包含"身份验证"作为上面代码中的LDAP

2 个答案:

答案 0 :(得分:2)

在通过非默认身份验证机制进行连接时,需要何时指定身份验证机制。您可以使用Authentication设置在ODBC连接字符串中执行此操作。

在你的情况下:

conn.Open "Driver={Teradata};" & _
  "DBCName=" & TDServer & ";" & _
  "Database=" & TDDb & ";" & _
  "Uid=" & TDUname & ";" & _
  "Pwd=" & TDPword & ";" & _
  "Authentication=LDAP"

您的连接字符串中需要考虑的其他一些非常有用的设置:

SessionMode。这应该默认为“Teradata”,但我想指定以防万一,因为如果你发现自己处于ANSI模式,你必须更加明确你的SQL。

  "SessionMode=Teradata" 

CharSet默认为ASCII,可能对您有用,但如果您有任何Unicode数据,则需要在连接字符串中指定Unicode字符集以避免Unicode字符被缩减为ASCII(问号和空框是常见的替代品)

  "CharSet=UTF8"

作为一个例子,这是我多年来一直用来处理LDAP连接的连接函数:

Public adoConn as ADODB.Connection
Private username as string
Private password as string

<other supporting functions here like getUsername and getPassword>

Function openConnection() As Boolean
    Dim ServerName As String, DatabaseName As String
    Set adoConn = New ADODB.Connection

    On Error GoTo ErrorHandler
startOver:
    If m_db.username = "" Or m_db.password = "" Then
        getUsername
        getPassword
    End If

    'ldap
    adoConn.Open "SessionMode=Teradata;Driver=Teradata;Authentication=LDAP;DBCName=mydb.somewhere.com;Database=mydb;CharSet=UTF8;Uid=" & username & ";Pwd=" & password        

    Exit Function
ErrorHandler:
     If Err.Number = -2147217843 Then
        yesno = MsgBox("Your username or password was incorrect." & vbCrLf & vbCrLf & "Try Again?", vbYesNo)
        If yesno = vbYes Then
            m_db.password = ""
            GoTo startOver
        End If
    End If
    Debug.Print Err.Description, Err.Number        
End Function

使用可使用参数切换ldap功能的版本进行更新:

Public adoConn as ADODB.Connection
Private username as string
Private password as string

<other supporting functions here like getUsername and getPassword>

Function openConnection(ldap as boolean) As Boolean
    Dim ServerName As String
    Dim DatabaseName As String
    Dim strConnection as string
    Set adoConn = New ADODB.Connection

    On Error GoTo ErrorHandler
startOver:
    If m_db.username = "" Or m_db.password = "" Then
        getUsername
        getPassword
    End If

    'ldap        
    If ldap Then 
        strConnection = "SessionMode=Teradata;Driver=Teradata;Authentication=LDAP;DBCName=mydb.somewhere.com;Database=mydb;CharSet=UTF8;Uid=" & username & ";Pwd=" & password  
    Else 'not ldap      
         strConnection = "SessionMode=Teradata;Driver=Teradata;DBCName=mydb.somewhere.com;Database=mydb;CharSet=UTF8;Uid=" & username & ";Pwd=" & password  
    End If

    'open the connection
    adoConn.Open strConnection

    Exit Function
ErrorHandler:
     If Err.Number = -2147217843 Then
        yesno = MsgBox("Your username or password was incorrect." & vbCrLf & vbCrLf & "Try Again?", vbYesNo)
        If yesno = vbYes Then
            m_db.password = ""
            GoTo startOver
        End If
    End If
    Debug.Print Err.Description, Err.Number        
End Function

您现在可以这样称呼:

Set connTeradataProd = openConnection(True) 'for ldap
Set connTeradataStage = openConnection(False) 'for default auth

答案 1 :(得分:0)

除了上面提供的解决方案之外,尝试了另外一种技术,为身份验证机制分配一个变量,并使用if else循环我们可以运行相同的

 If (LDAPString = "LDAP") Then

 Conn.Open "Driver={Teradata};" & _
"DBCName=" & TDServer & ";" & _
"Database=" & TDDb & ";" & _
"Uid=" & TDUname & ";" & _
"Pwd=" & TDPword & ";" & _
"Authentication=LDAP"

 Else

 Conn.Open "Driver={Teradata};" & _
"DBCName=" & TDServer & ";" & _
"Database=" & TDDb & ";" & _
"Uid=" & TDUname & ";" & _
"Pwd=" & TDPword & ""

End If