Powershell 5.1 使用 Oracle Data Provider for .NET,查询结果不在表中

发布于 2025-01-10 11:50:33 字数 2307 浏览 0 评论 0原文

我正在使用 SQLdeveloper 时尝试查询测试数据库

 从测试中选择 *

结果显示我的表格为

<前><代码> USER_ID 用户名 ---------- -------------------- 1001 用户1 1002 用户2

但是,当我尝试使用 Powershell ISE(即运行以下脚本的 PS 5.1)时

# 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() }
}

,我只能将其作为对象获取,无法将其作为表格获取。

USER_ID
1001
1002
USERNAME
user1
user2

如何将结果作为表格获取?先感谢您。

对于 $reader = $cmd.ExecuteReader() 之后的 $reader。我得到了

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

迷荒 2025-01-17 11:50:33

我认为使用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'} | ....

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'} | ....
偏爱你一生 2025-01-17 11:50:33

找到一种简单的方法

$dataset = New-Object system.Data.DataSet 
$dataadapter = New-Object Oracle.ManagedDataAccess.Client.OracleDataAdapter($cmd)
[void]$dataadapter.fill($dataset) 
$dataset.Tables[0]

感谢@filimonic和https://docs.oracle。 com/database/121/ODPNT/OracleCommandBuilderClass.htm#ODPNT655

Find an easy way

$dataset = New-Object system.Data.DataSet 
$dataadapter = New-Object Oracle.ManagedDataAccess.Client.OracleDataAdapter($cmd)
[void]$dataadapter.fill($dataset) 
$dataset.Tables[0]

Thanks @filimonic and https://docs.oracle.com/database/121/ODPNT/OracleCommandBuilderClass.htm#ODPNT655

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文