![]() |
VOOZH | about |
The CData Cmdlets for SAP HANA are standard PowerShell cmdlets that make it easy to accomplish data cleansing, normalization, backup, and other integration tasks by enabling real-time and bidirectional access to SAP HANA.
The Cmdlets are not only a PowerShell interface to SAP HANA, but also an SQL interface; this tutorial shows how to use both to create, retrieve, update, and delete SAP HANA data. We also show examples of the ADO.NET equivalent, which is possible with the CData ADO.NET Provider for SAP HANA. To access SAP HANA data from other .NET applications, like LINQPad, use the CData ADO.NET Provider for SAP HANA.
Once you have acquired the necessary connection properties, accessing SAP HANA data in PowerShell can be enabled in three steps.
Set the Server, Database and Port properties to specify the address of your SAP Hana database to interact with. Set the User and the Password properties to authenticate to the server.
Install the module:
Install-Module SAPHANACmdlets
Connect:
$saphana = Connect-SAPHANA -User "$User" -Password "$Password" -Server "$Server" -Database "$Database"
Search for and retrieve data:
$name = "TestBucket" $buckets = Select-SAPHANA -Connection $saphana -Table "Buckets" -Where "Name = `'$Name`'" $buckets
You can also use the Invoke-SAPHANA cmdlet to execute SQL commands:
$buckets = Invoke-SAPHANA -Connection $saphana -Query 'SELECT * FROM Buckets WHERE Name = @Name' -Params @{'@Name'='TestBucket'}
Load the provider's assembly:
[Reflection.Assembly]::LoadFile("C:\Program Files\CData\CData ADO.NET Provider for SAP HANA\lib\System.Data.CData.SAPHANA.dll")
Connect to SAP HANA:
$conn= New-Object System.Data.CData.SAPHANA.SAPHANAConnection("User=system;Password=mypassword;Server=localhost;Database=systemdb;")
$conn.Open()
Instantiate the SAPHANADataAdapter, execute an SQL query, and output the results:
$sql="SELECT Name, OwnerId from Buckets"
$da= New-Object System.Data.CData.SAPHANA.SAPHANADataAdapter($sql, $conn)
$dt= New-Object System.Data.DataTable
$da.Fill($dt)
$dt.Rows | foreach {
Write-Host $_.name $_.ownerid
}
Update-SAPHANA -Connection $SAPHANA -Columns @('Name','OwnerId') -Values @('MyName', 'MyOwnerId') -Table Buckets -Id "MyId"
$cmd = New-Object System.Data.CData.SAPHANA.SAPHANACommand("UPDATE Buckets SET Name='TestBucket' WHERE Id = @myId", $conn)
$cmd.Parameters.Add((New-Object System.Data.CData.SAPHANA.SAPHANAParameter("@myId","10456255-0015501366")))
$cmd.ExecuteNonQuery()
Add-SAPHANA -Connection $SAPHANA -Table Buckets -Columns @("Name", "OwnerId") -Values @("MyName", "MyOwnerId")
$cmd = New-Object System.Data.CData.SAPHANA.SAPHANACommand("INSERT INTO Buckets (Name) VALUES (@myName)", $conn)
$cmd.Parameters.Add((New-Object System.Data.CData.SAPHANA.SAPHANAParameter("@myName","TestBucket")))
$cmd.ExecuteNonQuery()
Remove-SAPHANA -Connection $SAPHANA -Table "Buckets" -Id "MyId"
$cmd = New-Object System.Data.CData.SAPHANA.SAPHANACommand("DELETE FROM Buckets WHERE Id=@myId", $conn)
$cmd.Parameters.Add((New-Object System.Data.CData.SAPHANA.SAPHANAParameter("@myId","001d000000YBRseAAH")))
$cmd.ExecuteNonQuery()
CodeProject
Download a free trial of the SAP HANA Data Provider to get started:
Download NowLearn more:
👁 SAP HANA IconRapidly create and deploy powerful .NET applications that integrate with SAP HANA databases.