sql - 使用Powershell访问远程Oracle数据库

标签 sql oracle powershell database-connection nuget-package

我需要能够连接到我的网络上基于 Windows 7 的 Oracle 服务器(32 位,Oracle XE)。我需要连接的机器运行 Windows 7 64 位,两台机器上都安装了 Powershell。

我已在 64 位计算机上安装了 Oracle 32 位客户端,并在两台计算机上安装了 SQL Developer。我想创建一个连接 Oracle 数据库并运行简单 SELECT 查询的脚本。但我无法连接。

我尝试过使用 ODAC(我想我必须安装 Visual Studio 才能使用它,因为安装失败)。我听说 OleBD 可能会容易得多。我想用 TNS 来做是可能的。有人可以在这里给我任何指导吗?我有一本关于 Powershell 和 Oracle 的书,但我仍然很困惑,我无法通过第一阶段。

任何帮助将不胜感激。

最佳答案

这是我在 2015 年使用的一个小例子。

# Ora002.ps1
# Need installation of ODAC1120320Xcopy_x64.zip 
# The 32 bit version also exists

# Load the good assembly
Add-Type -Path "C:\oracle\odp.net\bin\4\Oracle.DataAccess.dll"

# Connexion string
$compConStr = "Data Source=(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=10.213.5.123)(PORT=1609)))(CONNECT_DATA=(SERVER=DEDICATED)(SERVICE_NAME=COMPEIERE)));User Id=TheLogin;Password=ThePassword;"

# Connexion
$oraConn= New-Object Oracle.DataAccess.Client.OracleConnection($compConStr)
$oraConn.Open()

# Requête SQL
$sql1 = @"
SELECT XX_MYSESSION_ID FROM XX_SILOGIXWSLOG 
  WHERE xx_name='customer_log'
  AND xx_param_4 IS NOT NULL
"@

$command1 = New-Object Oracle.DataAccess.Client.OracleCommand($sql1,$oraConn)

# Execution
$reader1=$command1.ExecuteReader()

$n = 0
while ($reader1.read())
{
  $reader1["XX_MYSESSION_ID"]  
}

# Fermeture de la conexion
$reader1.Close()
$oraConn.Close()

Write-Output $retObj

----- 2017 年秋季编辑 -----

有一段时间,Oracle 为 .NET 编辑了一个完全托管的 DLL,可通过 Nugets 获取:

# Download the package if it's not on the disk    
$version = '12.2.1100'
try
{
  if (! $(Test-Path ".\NugetPackages\Oracle.ManagedDataAccess.$version\lib\net40\Oracle.ManagedDataAccess.dll"))
  {
    $ManagedDataAccess = Install-Package Oracle.ManagedDataAccess -Destination ".\NugetPackages" -Force -Source 'https://www.nuget.org/api/v2' -ProviderName NuGet -RequiredVersion $version -ErrorAction SilentlyContinue
  }
  Add-Type -Path ".\NugetPackages\Oracle.ManagedDataAccess.$version\lib\net40\Oracle.ManagedDataAccess.dll"
}
catch [System.Management.Automation.ParameterBindingException]
{
  $global:OracleError = New-Object PSCustomObject -Property @{"StackTrace"=$_.ScriptStackTrace;"Detail" = "Ligne $($_.InvocationInfo.ScriptLineNumber) : $($_.exception.message)";"TimeStamp"=([datetime]::Now)}
  $log = $null
}

# Connexion
$oraConn= New-Object Oracle.ManagedDataAccess.Client.OracleConnection (($compConStr)
$oraConn.Open()

# Requête SQL
$sql1 = @"
SELECT XX_MYSESSION_ID FROM XX_SILOGIXWSLOG 
  WHERE xx_name='customer_log'
  AND xx_param_4 IS NOT NULL
"@

$command1 = New-Object Oracle.ManagedDataAccess.Client.OracleCommand($sql1,$oraConn)

# Execution
$reader1=$command1.ExecuteReader()

$n = 0
while ($reader1.read())
{
  $reader1["XX_MYSESSION_ID"]  
}

# Fermeture de la conexion
$reader1.Close()
$oraConn.Close()

Write-Output $retObj

关于sql - 使用Powershell访问远程Oracle数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30697794/

相关文章:

MySQL。通过从每行获取输入值的子查询更新表中特定列的所有行

sql - 使所有单词在每个位置都有特殊字符列表 SQL Server

sql - Oracle-选择字段具有小写字符的位置

sql - Oracle 触发器而不是更新和 ORA-22816 错误

c# - 我不明白我遇到的 NewNotImplementedException 运行时错误

windows - 在 PowerShell 中运行 openssl 命令

sql - 为什么 sql case 不适用于 int 列的 null 条件

sql - Not a Group by expression 错误 [连接表]

oracle - Oracle和PostgreSQL中Write Skew异常不回滚事务

function - powershell:如何从 [ref] 变量中写入主机值