Powershell 5.1 使用 Oracle Data Provider for .NET,查询结果不在表中
我正在使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为使用DataTable切换更好
I think it's better to switch using DataTable
找到一种简单的方法
感谢@filimonic和https://docs.oracle。 com/database/121/ODPNT/OracleCommandBuilderClass.htm#ODPNT655
Find an easy way
Thanks @filimonic and https://docs.oracle.com/database/121/ODPNT/OracleCommandBuilderClass.htm#ODPNT655