Connecting SAP HANA DB from powershell



我使用以下Power Shell代码从安装了HANA客户端的主机连接到HANA实例-

    function Get-OLEDBData ($connectstring, $sql) {
    $OLEDBConn = New-Object System.Data.OleDb.OleDbConnection($connectstring)
    $OLEDBConn.open()
    $readcmd = New-Object system.Data.OleDb.OleDbCommand($sql,$OLEDBConn)
    $readcmd.CommandTimeout = '300'
    $da = New-Object system.Data.OleDb.OleDbDataAdapter($readcmd)
    $dt = New-Object system.Data.datatable
    [void]$da.fill($dt)
    $OLEDBConn.close()
    return $dt
    }
    $hdbSqlCmd = "myquery"
    $hdbConnectionString = "Driver={HDBODBC};ServerNode=myserver:30015;UID=myuser;PWD=mypasswd;"
    Get-OLEDBData $hdbConnectionString $hdbSqlCmd**

但我得到以下错误-

   New-Object : Exception calling ".ctor" with "1" argument(s): "An OLE DB Provider was not specified in the ConnectionString.  An example would be, 'Provider=SQLOLEDB;'."
   At C:myspacehana_connect_1.ps1:5 char:27+    $OLEDBConn = New-Object <<<<  System.Data.OleDb.OleDbConnection($connectstring)
+ CategoryInfo          : InvalidOperation: (:) [New-Object],        MethodInvocationException
+ FullyQualifiedErrorId : ConstructorInvokedThrowException,Microsoft.PowerShell.Commands.NewObjectCommand

当我将连接字符串更改为Provider而不是Driver-时

   $hdbConnectionString = "Provider={HDBODBC};ServerNode=myserver:30015;UID=myuser;PWD=mypasswd;"

我得到以下错误-

   Exception calling "Open" with "0" argument(s): "The '{HDBODBC}' provider is not registered on the local machine."
   At C:myspacehana_connect_1.ps1:6 char:19
   +    $OLEDBConn.open <<<< ()
  + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException + FullyQualifiedErrorId : DotNetMethodException

即使我更改为32位驱动程序-HDBODBC32,也会显示错误,并表示HDBODBC32-未注册。

我现在迷路了,帮帮别人吧!!

SAP HANA ODBC驱动程序是而不是OLEDB驱动程序。因此,您必须使用ODBC API来代替:

 function Get-ODBCBData ($connectstring, $sql) {
    $Conn = New-Object System.Data.odbc.ODBCconnection($connectstring)
    $Conn.open()
    $readcmd = New-Object system.Data.odbc.ODBCCommand($sql,$Conn)
    $readcmd.CommandTimeout = '300'
    ...
    $hdbConnectionString = "Driver={HDBODBC};ServerNode=myserver:30015;UID=myuser;PWD=mypasswd;"
    Get-ODBCBData $hdbConnectionString $hdbSqlCmd**

应该起作用。

相关内容

  • 没有找到相关文章

最新更新