.netpowershellodac

Powershell 5.1 using Oracle Data Provider for .NET, query result not in table


I'm trying to query a test database, while I am using SQLdeveloper

    SELECT * FROM TESTABLE

The result show my table as

       USER_ID USERNAME            
    ---------- --------------------
          1001 user1               
          1002 user2

However, when I try to use Powershell ISE, which is PS 5.1 running the following script

# This code is modified from https://purple.telstra.com.au/blog/using-powershell-to-query-oracle-dbs-without-using-the-oracle-client-oracle-data-provider-for-net
# ODAC downloaded form https://download.oracle.com/otn/other/ole-oo4o/ODP.NET_Managed_ODAC122cR1.zip?AuthParam=1645855877_652ef8e00f092e80aca26c6da393eb7d
# I've run install_odpm.bat and extracted the downlaoded files to C:\Oracle\odp.net


# Path to ODAC.NET Installation
Add-Type -Path "C:\Oracle\odp.net\managed\common\Oracle.ManagedDataAccess.dll"

$reader = $false
# SQL DB Username, Password and DataSource Alias (as per tnsnames.ora)
# I've grant demouser SYSDA permission
$username = "demouser"
$password = "demouser"
# Alias from TNSNames.ora
# Actually I find this in cmd to find XEPDB1
# lsnrctl status 
$datasource = "localhost/XEPDB1"

$connectionstring = 'User Id=' + $username + ';Password=' + $password + ';Data Source=' + $datasource 
$queryStatment = "SELECT * FROM TESTABLE"

try{
    $con = New-Object Oracle.ManagedDataAccess.Client.OracleConnection($connectionstring)
    $con.Open()

    $cmd = $con.CreateCommand()
    $cmd.CommandType = "text"
    $cmd.CommandText = $queryStatment

    $reader = $cmd.ExecuteReader()
    $reader.Getname(0)
    #$reader.GetDataTypeName(0)
    while ($reader.Read()){
        $reader.GetDecimal(0)
        }
    $reader = $cmd.ExecuteReader()
    $reader.Getname(1)
    #$reader.GetDataTypeName(1)
    while ($reader.Read()){
        $reader.GetString(1);
        }
    $reader = $cmd.ExecuteReader()    
   }
catch {
    Write-Error (“Database Exception: {0}`n{1}” -f `
        $con.ConnectionString, $_.Exception.ToString())
} finally{
    if ($con.State -eq ‘Open’) { $con.close() }
}

I can only get it as object, can't get it as a table

USER_ID
1001
1002
USERNAME
user1
user2

How can I get my result as a table? Thank you in advance.

For $reader after $reader = $cmd.ExecuteReader(). I got

FieldCount
----------
         2
         2

Solution

  • I think it's better to switch using DataTable

    #region Get Oracle data
    
    Add-Type -Path "$($PSScriptRoot)\ODP.NET\managed\common\Oracle.ManagedDataAccess.dll" -ErrorAction Stop
    
    $oCSBuilder = New-Object -TypeName 'Oracle.ManagedDataAccess.Client.OracleConnectionStringBuilder' -ErrorAction Stop
    $oCSBuilder['user id'] = 'userid'
    $oCSBuilder['password'] = 'xxx'
    $oCSBuilder['Data Source'] = '(description=(address=(protocol=tcp)(host=x.x.x)(port=1521))(connect_data=(server=dedicated)(service_name=xxx)))'
    
    # Create DataAdapter
    $oDA = New-Object -TypeName 'Oracle.ManagedDataAccess.Client.OracleDataAdapter' -ArgumentList @(
        "select * from x.x where xx = 'xxx'",
        $oCSBuilder.ConnectionString
        ) -ErrorAction Stop
    
    # Create DataTable
    $oDT = New-Object -TypeName 'System.Data.DataTable' -ErrorAction Stop
    # Fill DataTable
    $oDA.Fill($oDT) | Out-Null
    #Dispose dataAdapter
    $oDA.Dispose()
     
    $data = @($odt.Rows | Where-Object {$_.PROPERTY -eq 'Value'} | ....