从 powershell 连接 SAP HANA 数据库
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:\myspace\hana_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:\myspace\hana_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**
应该可以。
我正在使用以下 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:\myspace\hana_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:\myspace\hana_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**
应该可以。