How to run a SQL Plus script in PowerShell

后端 未结 5 1915
鱼传尺愫
鱼传尺愫 2020-12-14 23:36

I am trying to log in to the the Oracle DB using PowerShell and run a script called \"C:\\Users\\Administrator\\Desktop\\oracle\\OracleCleanTest.sql\", When I execute the PS

5条回答
  •  臣服心动
    2020-12-15 00:06

    You can use .NET Oracle library DLL, just make sure you have the required DLL file under the lib folder

    Add-Type -Path "lib\Oracle.ManagedDataAccess.dll"
    
    $query = "select  1 as Col1, 2 as Col2, 3 as Col3 from dual
              union
              select  4 as Col1, 5 as Col2, 6 as Col3 from dual
              union
              select  7 as Col1, 8 as Col2, 9 as Col3 from dual"
    
    $cn   = New-Object Oracle.ManagedDataAccess.Client.OracleConnection -ArgumentList "TNS-ConnectionString-Here"
    $cmd  = New-Object Oracle.ManagedDataAccess.Client.OracleCommand    -ArgumentList $query
    
    $cmd.Connection = $cn
    
    try {
        $cn.Open()
    
        $reader = $cmd.ExecuteReader()
    
        while ($reader.Read()) {
            $col1 = $reader["Col1"]
            $col2 = $reader["Col2"]
            $col3 = $reader["Col3"]
    
            Write-Host $col1, $col2, $col3
        }
    
    } catch {
        Write-Error $_.Exception.Message
    } finally {
        $cmd.Dispose()
        $cn.Dispose()
    }
    

提交回复
热议问题