.bat and PowerShell .ps1 are the only "programming" environments (again, out-of-the-box, meaning no additional installation required).New-Object cmdlet. I do not know if this framework is out-of-the-box on each installation, but the version v4.0 is available on mine.Get-ChildItem -Attributes Directory $Env:WinDir\Microsoft.NET\Framework\cmd's dir %windir%\Microsoft.NET\Framework /AD. In this post I am going to do all commands using PowerShell, but if you want to run them from the Command Line, then prefix them with powershell -c.Get-OdbcDriver -Name "HDB*"
Attribute. And I was right. After a bit of further experimentation, I was able to get nested information displayed too.(Get-OdbcDriver -Name "HDB*").GetValue(1).Attribute
Get-OdbcDsn -DriverName "HDBODBC*"
Get-OdbcDsn -Name "hxehost" | ConvertTo-Json

Btw, SAP provides two useful troubleshooting utilities as well, which you can find in the installation folder of HANA Clients:hdbodbc_cons.exeandodbcreg.exe. Their use in not part of the today's post.
$host.privatedata.errorbackgroundcolor= $host.ui.rawui.BackgroundColor$conn = new-object System.Data.Odbc.OdbcConnection
$conn.connectionstring = "Dsn=SAP HANA Cloud;UID={User1};PWD={Password1};CurrentSchema=HOTEL"
$conn.Open()
Write-Output $conn
DSN, UID and PWD are ODBC standard attribute keywords. CurrentSchema is a driver-defined attribute keyword, in this case, HANA-specific attribute.$cmd = New-object System.Data.Odbc.OdbcCommand("SELECT * FROM HOTEL",$conn)
$dtab = New-Object System.Data.DataTable
$adap = New-Object System.Data.Odbc.OdbcDataAdapter($cmd)
$nrr = $adap.fill($dtab)
Write-Output "Records selected: ${nrr}"
Write-Output $dtab
$conn.Close()
Write-Output $conn
$conn = new-object System.Data.Odbc.OdbcConnection
$conn.connectionstring = "Driver={HDBODBC};SERVERNODE={hxehost:39015};UID={User1};PWD={Password1};encrypt={True};sslValidateCertificate=False"
$conn.Open()
Write-Output $conn
$cmd = New-object System.Data.Odbc.OdbcCommand("SELECT * FROM HOTEL.HOTEL",$conn)
$dtab = New-Object System.Data.DataTable
$adap = New-Object System.Data.Odbc.OdbcDataAdapter($cmd)
$nrr = $adap.fill($dtab)
Write-Output "Records selected: ${nrr}"
Write-Output $dtab
$conn.Close()
Write-Output $connHOTEL schema to query from HOTEL table, as this time the connection string did not have CurrentSchema property defined.queryHana.ps1 in the new subfolder odbc.New-Item -Path "$env:USERPROFILE\HANAClientsTutorial" -Name "odbc" -ItemType "directory" -Force
cd "$env:USERPROFILE\HANAClientsTutorial\odbc"
New-Item -Name "queryHana.ps1" -ItemType "file" -Force
queryHana.ps1 script file.param(
[String] $hdbhost = "hxehost",
[Int32] $hdbport = 39015,
[String] $hdbuser = "User1",
[String] $hdbpwd = "Password1"
)
$hdbsql = "SELECT * FROM HOTEL.HOTEL"
$conn = new-object System.Data.Odbc.OdbcConnection
$conn.connectionstring = "Driver={HDBODBC};SERVERNODE={${hdbhost}:${hdbport}};UID={${hdbuser}};PWD={${hdbpwd}};encrypt={True};sslValidateCertificate=False"
try {
$conn.open()
$cmd = New-object System.Data.Odbc.OdbcCommand($hdbsql,$conn)
$dtab = New-Object System.Data.DataTable
$adap = New-Object System.Data.Odbc.OdbcDataAdapter($cmd)
$nrr = $adap.fill($dtab)
Write-Output "Records selected: ${nrr}"
Write-Output $dtab
$conn.Close()
} catch {
Write-Output $_.Exception.Message
}
Get-Help. It collects and shows parameters accepted by the script. Get-Help .\queryHana.ps1
queryHana.ps1 connects to a HANA Express instance, I can call the script as well with my HANA Cloud instance host and port (and user plus password, if needed) as parameters. And PowerShell automatically supports tab autocompletion for parameter names after you type - ?.\queryHana.ps1 `
>> -hdbhost 2246ed61-81df-48e8-9711-323311f7613f.hana.prod-eu20.hanacloud.ondemand.com `
>> -hdbport 443
hdbuserstore)USER1USERKEY.@ sign in your data source or in the connection string: servernode=@<KEY>.$conn = new-object System.Data.Odbc.OdbcConnection
$conn.connectionstring = "Driver={HDBODBC};SERVERNODE=@USER1USERKEY;CURRENTSCHEMA=HOTEL"
$conn.Open()
$cmd = New-object System.Data.Odbc.OdbcCommand("SELECT CURRENT_USER, CURRENT_SCHEMA FROM DUMMY",$conn)
$dset = New-Object System.Data.DataSet
$adap = New-Object System.Data.Odbc.OdbcDataAdapter($cmd)
$nrr = $adap.fill($dset)
Write-Output "Records selected: ${nrr}"
Write-Output $dset.Tables
$conn.Close()CURRENTSCHEMA in the connection string and its influence of the result. And note the use of System.Data.DataSet instead of System.Data.DataTable.
...it took me quite a while to get things right. One of the most confusing steps was the use of curly braces in connection strings. I reread a few times the required syntax of connection strings, but maybe it was already late. I will need to read this once again.
It is easy to get"ERROR [IM002] [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified"when assumed that because of spaces in it the name of the DSN attribute value should be in{}. Like in$conn.connectionstring = "DSN={SAP HANA Cloud}".
Because of"...no default driver specified"in the previous error, I made another mistake that kept me on toes, now because of the new error"ERROR [HY000] [SAP AG][LIBODBCHDB DLL][HDBODBC] General error;-10719 Connect failed (invalid SERVERNODE '')"when I added theDRIVERattribute keyword to the connection string. Like in$conn.connectionstring = "DRIVER={HDBODBC};Dsn={SAP HANA Cloud}".
I mean how would you assume something is wrong even after staring at this for an hour??
One more problem I was facing was with some strangeC:\Windows\ODBC.inifile although the whole configuration should be in the registryComputer\HKEY_CURRENT_USER\Software\ODBC\ODBC.INI.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
| User | Count |
|---|---|
| 46 | |
| 42 | |
| 38 | |
| 32 | |
| 28 | |
| 28 | |
| 27 | |
| 23 | |
| 23 | |
| 23 |