![]() |
VOOZH | about |
The CData Cmdlets for Oracle 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 Oracle.
The Cmdlets are not only a PowerShell interface to Oracle, but also an SQL interface; this tutorial shows how to use both to create, retrieve, update, and delete Oracle data. We also show examples of the ADO.NET equivalent, which is possible with the CData ADO.NET Provider for Oracle. To access Oracle data from other .NET applications, like LINQPad, use the CData ADO.NET Provider for Oracle.
Once you have acquired the necessary connection properties, accessing Oracle data in PowerShell can be enabled in three steps.
To connect to Oracle, you'll first need to update your PATH variable and ensure it contains a folder location that includes the native DLLs. The native DLLs can be found in the lib folder inside the installation directory. Once you've done this, set the following to connect:
Install the module:
Install-Module OracleOCICmdlets
Connect:
$oracleoci = Connect-OracleOCI -User "$User" -Password "$Password" -Server "$Server" -Port "$Port"
Search for and retrieve data:
$country = "US" $customers = Select-OracleOCI -Connection $oracleoci -Table "Customers" -Where "Country = `'$Country`'" $customers
You can also use the Invoke-OracleOCI cmdlet to execute SQL commands:
$customers = Invoke-OracleOCI -Connection $oracleoci -Query 'SELECT * FROM Customers WHERE Country = @Country' -Params @{'@Country'='US'}
Load the provider's assembly:
[Reflection.Assembly]::LoadFile("C:\Program Files\CData\CData ADO.NET Provider for Oracle\lib\System.Data.CData.OracleOCI.dll")
Connect to Oracle:
$conn= New-Object System.Data.CData.OracleOCI.OracleOCIConnection("User=myuser;Password=mypassword;Server=localhost;Port=1521;")
$conn.Open()
Instantiate the OracleOCIDataAdapter, execute an SQL query, and output the results:
$sql="SELECT CompanyName, City from Customers"
$da= New-Object System.Data.CData.OracleOCI.OracleOCIDataAdapter($sql, $conn)
$dt= New-Object System.Data.DataTable
$da.Fill($dt)
$dt.Rows | foreach {
Write-Host $_.companyname $_.city
}
Update-OracleOCI -Connection $OracleOCI -Columns @('CompanyName','City') -Values @('MyCompanyName', 'MyCity') -Table Customers -Id "MyId"
$cmd = New-Object System.Data.CData.OracleOCI.OracleOCICommand("UPDATE Customers SET Country='US' WHERE Id = @myId", $conn)
$cmd.Parameters.Add((New-Object System.Data.CData.OracleOCI.OracleOCIParameter("@myId","10456255-0015501366")))
$cmd.ExecuteNonQuery()
Add-OracleOCI -Connection $OracleOCI -Table Customers -Columns @("CompanyName", "City") -Values @("MyCompanyName", "MyCity")
$cmd = New-Object System.Data.CData.OracleOCI.OracleOCICommand("INSERT INTO Customers (Country) VALUES (@myCountry)", $conn)
$cmd.Parameters.Add((New-Object System.Data.CData.OracleOCI.OracleOCIParameter("@myCountry","US")))
$cmd.ExecuteNonQuery()
Remove-OracleOCI -Connection $OracleOCI -Table "Customers" -Id "MyId"
$cmd = New-Object System.Data.CData.OracleOCI.OracleOCICommand("DELETE FROM Customers WHERE Id=@myId", $conn)
$cmd.Parameters.Add((New-Object System.Data.CData.OracleOCI.OracleOCIParameter("@myId","001d000000YBRseAAH")))
$cmd.ExecuteNonQuery()
CodeProject
Download a free trial of the Oracle Data Provider to get started:
Download NowLearn more:
👁 Oracle IconRapidly create and deploy powerful .NET applications that integrate with Oracle databases.