LoginSignup
4
3

More than 1 year has passed since last update.

Powershell 7でODP.NET Core

Last updated at Posted at 2020-03-19

Powershell7.0は.NET Core 3.1を基板としています。
よって、Oracle.ManagedDataAccessではなく、Oracle.ManagedDataAccess.Coreを利用します。

参考: Oracle Data Provider for .NETバージョニング体系

ODP.NET Coreのインストール

この記事ではNugetから取得します。

参考: Oracle Data Provider for .NET Coreのインストール

PS E:\> Install-Package Oracle.ManagedDataAccess.Core -scope CurrentUser -Verbose

VERBOSE: Acquiring providers for assembly: C:\program files\powershell\7\Modules\PackageManagement\coreclr\netstandard2.0\Microsoft.PackageManagement.ArchiverProviders.dll
VERBOSE: Acquiring providers for assembly: C:\program files\powershell\7\Modules\PackageManagement\coreclr\netstandard2.0\Microsoft.PackageManagement.CoreProviders.dll
# 中略
VERBOSE: Performing the operation "Install Package" on target "Package 'Oracle.ManagedDataAccess.Core' version '2.19.60' from 'nuget.org'.".
The package(s) come(s) from a package source that is not marked as trusted.
Are you sure you want to install software from 'nuget.org'?
[Y] Yes [A] Yes to All [N] No [L] No to All [S] Suspend [?] Help (default is "No"): a
# 中略
Name                           Version          Source           Summary
----                           -------          ------           -------
Oracle.ManagedDataAccess.Core  2.19.60          nuget.org        Oracle Data Provider for .NET Core for Oracle Database

PS E:\> 

使い方

オラクルクライアントoracle11g2インストール済み環境の場合です。
変則的ですが、アセンブリはGACに登録せずPowershellのセッション毎に呼び出します。

参考
Oracle Data Provider for .NET Coreの構成
OracleConfiguration.TnsAdmin
OracleConnectionStringBuilderクラス


using namespace Oracle.ManagedDataAccess.Client

# ダウンロードしたライブラリのパスを生成
$sourcePath=Split-Path (Get-Package Oracle.ManagedDataAccess.Core).Source
$dllPath = Join-Path $sourcePath lib netstandard2.0 Oracle.ManagedDataAccess.dll
# ダウンロードしたライブラリを呼び出す
Add-Type -path $dllPath

# レジストリからtnsnames.oraが配置されているディレクトリを取得して設定
# ※文字列で直接設定する場合
# [OracleConfiguration]::TnsAdmin="C:\oracle\oracle11g2\product\11.2.0\client_1\NETWORK\ADMIN"
$tnsAdmin =Join-Path (Get-itemproperty (Join-Path HKLM: SOFTWARE ORACLE *)).ORACLE_HOME NETWORK ADMIN
[OracleConfiguration]::TnsAdmin = $tnsAdmin

# ConnectionStringBuilderを使用
$connStrSb = [OracleConnectionStringBuilder]::new()
$connStrSb.Add("USER ID","scott")
$connStrSb.Add("PASSWORD","tiger")
$connStrSb.Add("DATA SOURCE","oracle")
$conn = [OracleConnection]::new($connStrSb.ToString())

# DB接続
$conn.Open()

# 取得可能なスキーマ情報の一覧を表示
$conn.GetSchema()|Out-GridView

# 各スキーマで利用可能な制限値
$conn.GetSchema("Restrictions")|Out-GridView

# スキーマ情報表示
$conn.GetSchema("DataSourceInformation")|Out-GridView
$conn.GetSchema("Tables")|Out-GridView
$conn.GetSchema("Views")|Out-GridView
$conn.GetSchema("Indexes")|Out-GridView
$conn.GetSchema("Columns")|Out-GridView
$conn.GetSchema("IndexColumns")|Out-GridView
# 条件を指定してスキーマを取得
$conn.GetSchema("Views",@("OWNERNAME",$null))|Out-GridView

# 取得したスキーマ情報を加工
$Views = $conn.GetSchema("OWNERNAME",@("OWNER",$null))
$Views.Columns.ColumnName|Out-GridView
$Views.Select("VIEW_NAME = 'products'")|Out-GridView

# クエリ実行
# パラメータなしのコンストラクタがあるクラスはハッシュテーブルから生成可能
$adapter = [OracleDataAdapter]@{
    SelectCommand=[OracleCommand]@{
        CommandText="SELECT TOP 10 * FROM products"
        Connection=$conn
    }
}
# クエリ結果をDatasetにセット
$ds=[System.Data.DataSet]::new("ds")
$adapter.Fill($ds)

# 接続終了
$conn.Close()
$conn.Dispose()

普段はスキーマ情報の確認などで使ってます。

4
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
3