Home:ALL Converter>Read multiple rows from Oracle table

Read multiple rows from Oracle table

Ask Time:2017-12-05T17:28:15         Author:Obongo

Json Formatter

I am trying to read a table in Oracle from PowerShell and want to save them in an ArrayList. The connection is working but reading the any rows after the first doesn't work.

Here's what I'm trying to do.

$rows = New-Object System.Collections.ArrayList
class Table {
    [String] $name
    [String] $type
}

try {
    $oraConn.Open()
    $sql = [string]::Format("select name, type from source_table where type = 'running'")

    $oraCmd = New-Object System.Data.OracleClient.OracleCommand($sql, $oraConn)
    $reader = $oraCmd.ExecuteReader()

    #add tables to arraylist
    while ($reader.Read()) {
        $table = New-Object Table
        $table.name = $reader["name"];
        $table.type = $reader["type"];
        [void]$rows.Add($table)
    }
    Write-Host "rows collected"
}

My problem is, I only read the first row of the table, how can I tell Oracle to read them all? Would I have to countthem first and then query for each row?

I check the contents of $rows later in the code, it's not really relevant to the question since I know that this part works, so I left it out. I know that my query returns something because I tried it in Oracle.

Do I need a foreach loop? It would make sense but how can I tell Oracle to do that? Would I have to query for each row of the table and set a counter to query only one row at a time?

I hope someone can help me and point me in the right direction, since I'm already trying a long time to get my script working. I got most of the logic for my script, but if I can't load the rows into my list, my logic doesn't help me at all.

Author:Obongo,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/47650118/read-multiple-rows-from-oracle-table
yy