![]() |
VOOZH | about |
The CData Cmdlets for EnterpriseDB 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 EnterpriseDB.
The Cmdlets are not only a PowerShell interface to EnterpriseDB, but also an SQL interface; this tutorial shows how to use both to create, retrieve, update, and delete EnterpriseDB data. We also show examples of the ADO.NET equivalent, which is possible with the CData ADO.NET Provider for EnterpriseDB. To access EnterpriseDB data from other .NET applications, like LINQPad, use the CData ADO.NET Provider for EnterpriseDB.
Once you have acquired the necessary connection properties, accessing EnterpriseDB data in PowerShell can be enabled in three steps.
The following connection properties are required in order to connect to data.
You can also optionally set the following:
To authenticate using standard authentication, set the following:
You can leverage SSL authentication to connect to EnterpriseDB data via a secure session. Configure the following connection properties to connect to data:
Install the module:
Install-Module EnterpriseDBCmdlets
Connect:
$enterprisedb = Connect-EnterpriseDB -User "$User" -Password "$Password" -Database "$Database" -Server "$Server" -Port "$Port"
Search for and retrieve data:
$shipcountry = "USA" $orders = Select-EnterpriseDB -Connection $enterprisedb -Table "Orders" -Where "ShipCountry = `'$ShipCountry`'" $orders
You can also use the Invoke-EnterpriseDB cmdlet to execute SQL commands:
$orders = Invoke-EnterpriseDB -Connection $enterprisedb -Query 'SELECT * FROM Orders WHERE ShipCountry = @ShipCountry' -Params @{'@ShipCountry'='USA'}
Load the provider's assembly:
[Reflection.Assembly]::LoadFile("C:\Program Files\CData\CData ADO.NET Provider for EnterpriseDB\lib\System.Data.CData.EnterpriseDB.dll")
Connect to EnterpriseDB:
$conn= New-Object System.Data.CData.EnterpriseDB.EnterpriseDBConnection("User=postgres;Password=admin;Database=postgres;Server=127.0.0.1;Port=5444")
$conn.Open()
Instantiate the EnterpriseDBDataAdapter, execute an SQL query, and output the results:
$sql="SELECT ShipName, ShipCity from Orders"
$da= New-Object System.Data.CData.EnterpriseDB.EnterpriseDBDataAdapter($sql, $conn)
$dt= New-Object System.Data.DataTable
$da.Fill($dt)
$dt.Rows | foreach {
Write-Host $_.shipname $_.shipcity
}
Update-EnterpriseDB -Connection $EnterpriseDB -Columns @('ShipName','ShipCity') -Values @('MyShipName', 'MyShipCity') -Table Orders -Id "MyId"
$cmd = New-Object System.Data.CData.EnterpriseDB.EnterpriseDBCommand("UPDATE Orders SET ShipCountry='USA' WHERE Id = @myId", $conn)
$cmd.Parameters.Add((New-Object System.Data.CData.EnterpriseDB.EnterpriseDBParameter("@myId","10456255-0015501366")))
$cmd.ExecuteNonQuery()
Add-EnterpriseDB -Connection $EnterpriseDB -Table Orders -Columns @("ShipName", "ShipCity") -Values @("MyShipName", "MyShipCity")
$cmd = New-Object System.Data.CData.EnterpriseDB.EnterpriseDBCommand("INSERT INTO Orders (ShipCountry) VALUES (@myShipCountry)", $conn)
$cmd.Parameters.Add((New-Object System.Data.CData.EnterpriseDB.EnterpriseDBParameter("@myShipCountry","USA")))
$cmd.ExecuteNonQuery()
Remove-EnterpriseDB -Connection $EnterpriseDB -Table "Orders" -Id "MyId"
$cmd = New-Object System.Data.CData.EnterpriseDB.EnterpriseDBCommand("DELETE FROM Orders WHERE Id=@myId", $conn)
$cmd.Parameters.Add((New-Object System.Data.CData.EnterpriseDB.EnterpriseDBParameter("@myId","001d000000YBRseAAH")))
$cmd.ExecuteNonQuery()
CodeProject
Download a free trial of the EnterpriseDB Data Provider to get started:
Download NowLearn more:
👁 EnterpriseDB IconRapidly create and deploy powerful .NET applications that integrate with EnterpriseDB.