![]() |
VOOZH | about |
The CData Cmdlets for DB2 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 DB2.
The Cmdlets are not only a PowerShell interface to DB2, but also an SQL interface; this tutorial shows how to use both to create, retrieve, update, and delete DB2 data. We also show examples of the ADO.NET equivalent, which is possible with the CData ADO.NET Provider for IBM DB2. To access DB2 data from other .NET applications, like LINQPad, use the CData ADO.NET Provider for IBM DB2.
Once you have acquired the necessary connection properties, accessing DB2 data in PowerShell can be enabled in three steps.
Set the following properties to connect to DB2:
You will also need to install the corresponding DB2 driver:
On Windows, installing the IBM Data Server Provider is sufficient, as the installation registers it in the machine.config.
In the Java version, place the IBM Data Server Driver JAR in the www\WEB-INF\lib\ folder for this application.
Install the module:
Install-Module DB2Cmdlets
Connect:
$db2 = Connect-DB2 -Server "$Server" -Port "$Port" -User "$User" -Password "$Password" -Database "$Database"
Search for and retrieve data:
$shipcity = "New York" $orders = Select-DB2 -Connection $db2 -Table "Orders" -Where "ShipCity = `'$ShipCity`'" $orders
You can also use the Invoke-DB2 cmdlet to execute SQL commands:
$orders = Invoke-DB2 -Connection $db2 -Query 'SELECT * FROM Orders WHERE ShipCity = @ShipCity' -Params @{'@ShipCity'='New York'}
Load the provider's assembly:
[Reflection.Assembly]::LoadFile("C:\Program Files\CData\CData ADO.NET Provider for IBM DB2\lib\System.Data.CData.DB2.dll")
Connect to DB2:
$conn= New-Object System.Data.CData.DB2.DB2Connection("Server=10.0.1.2;Port=50000;User=admin;Password=admin;Database=test;")
$conn.Open()
Instantiate the DB2DataAdapter, execute an SQL query, and output the results:
$sql="SELECT OrderName, Freight from Orders"
$da= New-Object System.Data.CData.DB2.DB2DataAdapter($sql, $conn)
$dt= New-Object System.Data.DataTable
$da.Fill($dt)
$dt.Rows | foreach {
Write-Host $_.ordername $_.freight
}
Update-DB2 -Connection $DB2 -Columns @('OrderName','Freight') -Values @('MyOrderName', 'MyFreight') -Table Orders -Id "MyId"
$cmd = New-Object System.Data.CData.DB2.DB2Command("UPDATE Orders SET ShipCity='New York' WHERE Id = @myId", $conn)
$cmd.Parameters.Add((New-Object System.Data.CData.DB2.DB2Parameter("@myId","10456255-0015501366")))
$cmd.ExecuteNonQuery()
Add-DB2 -Connection $DB2 -Table Orders -Columns @("OrderName", "Freight") -Values @("MyOrderName", "MyFreight")
$cmd = New-Object System.Data.CData.DB2.DB2Command("INSERT INTO Orders (ShipCity) VALUES (@myShipCity)", $conn)
$cmd.Parameters.Add((New-Object System.Data.CData.DB2.DB2Parameter("@myShipCity","New York")))
$cmd.ExecuteNonQuery()
Remove-DB2 -Connection $DB2 -Table "Orders" -Id "MyId"
$cmd = New-Object System.Data.CData.DB2.DB2Command("DELETE FROM Orders WHERE Id=@myId", $conn)
$cmd.Parameters.Add((New-Object System.Data.CData.DB2.DB2Parameter("@myId","001d000000YBRseAAH")))
$cmd.ExecuteNonQuery()
CodeProject
Download a free trial of the IBM DB2 Data Provider to get started:
Download NowLearn more:
👁 IBM DB2 IconRapidly create and deploy powerful .NET applications that integrate with IBM DB2.