Automate Oracle Integration Tasks from PowerShell
The CData ADO.NET Provider for Oracle is a standard ADO.NET Provider that make it easy to accomplish data cleansing, normalization, backup, and other integration tasks by enabling real-time and bidirectional access to Oracle.
ADO.NET Provider
The ADO.NET Provider provides a SQL interface for Oracle; this tutorial shows how to use the Provider to create, retrieve, update, and delete Oracle data.
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:
- Port: The port used to connect to the server hosting the Oracle database.
- User: The user Id provided for authentication with the Oracle database.
- Password: The password provided for authentication with the Oracle database.
- Service Name: The service name of the Oracle database.
-
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 Oracle Data
$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()
Insert Oracle Data
$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()
Delete Oracle Data
$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