I have prepared code to test ODP.NET by establishing a database connection:
I have 64-bit and 32 -bit Oracle clients installed on my machine and when I am querying I was able to connect to the database successfully but I figured out that when I am calling the oracle data access.dll i.e.,
[System.Reflection.Assembly]::LoadFile("C:\oracle\"+$OracleHome+"\odp.net\bin\2.x\Oracle.DataAccess.dll")
it is always loading 64-bit dataaccess.dll when I am using 32 bit oracle client also.
GAC Version Location
--- ------- --------
True v2.0.50727 C:\Windows\assembly\GAC_64\Oracle.DataAccess\2.112.4.0__89b483f429c47342\Oracle.DataAccess.dll
I tried to load the 32 bit forcibly byhardcoding the value, but i got below error:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Exception calling "LoadFile" with "1" argument(s): "Could not load file or assembly 'Oracle.DataAccess, Version=2.112.4.0, Culture=neutral,
PublicKeyToken=89b483f429c47342' or one of its dependencies. An attempt was made to load a program with an incorrect format."
At line:22 char:1
+ [System.Reflection.Assembly]::LoadFile("C:\Windows\assembly\GAC_32\Oracle.DataAc ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : BadImageFormatException
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Later i realized that i was using Powershell_ISE.exe and i tired to execute the same code with PowerShell_ISE(x86).exe
and it used 32 bit OracleDataaccess.dll.My Question here is can't we load the oracle dataaccess.dll either 32/64 bit in one powershell_ise.exe, do i have to always call the 32 bit PowerShell_ISE(x86).exe for loading the 32bit dll?
My Full Code is here:
Write-Host "#############################################################################################"
Write-Host "#### Testing ODP.NET Connection ####"
Write-Host "#############################################################################################"
"`n"
Set-StrictMode -Version Latest
function ConvertFromSecureToPlain {
param( [Parameter(Mandatory=$true)][System.Security.SecureString] $SecurePassword)
# Create a "password pointer".
$PasswordPointer = [Runtime.InteropServices.Marshal]::SecureStringToBSTR($SecurePassword)
# Get the plain text version of the password.
$PlainTextPassword = [Runtime.InteropServices.Marshal]::PtrToStringAuto($PasswordPointer)
# Free the pointer.
[Runtime.InteropServices.Marshal]::ZeroFreeBSTR($PasswordPointer)
# Return the plain text password.
$PlainTextPassword
}
$OracleHome= Read-Host "Type the Oracle Home you would like to use (i.e, v112040b64):"
[System.Reflection.Assembly]::LoadFile("C:\oracle\"+$OracleHome+"\odp.net\bin\2.x\Oracle.DataAccess.dll")
Write-Host "-------------------------------------------------------------------------------------------------------------------------"
$dbid=Read-Host "Enter Database Name:"
$userid= read-host "Enter UserId:"
$password = read-host "Enter DB password:" -AsSecureString
$PlainTextPassword = ConvertFromSecureToPlain $Password
$ConnectionString = "User ID="+$userid+";Password="+$PlainTextPassword+";Data Source="+$dbid+";Persist Security Info=True"
$CommandText = "SELECT * FROM SQLPLUSPRODUCTS"
#[Reflection.Assembly]::LoadFile($AssemblyFile) | Out-Null
$OracleConnection = New-Object -TypeName Oracle.DataAccess.Client.OracleConnection
$OracleConnection.ConnectionString = $ConnectionString
$OracleConnection.Open()
$OracleCommand = New-Object -TypeName Oracle.DataAccess.Client.OracleCommand
$OracleCommand.CommandText = $CommandText
$OracleCommand.Connection = $OracleConnection
$OracleDataAdapter = New-Object -TypeName Oracle.DataAccess.Client.OracleDataAdapter
$OracleDataAdapter.SelectCommand = $OracleCommand
$DataSet = New-Object -TypeName System.Data.DataSet
$OracleDataAdapter.Fill($DataSet) | Out-Null
$OracleDataAdapter.Dispose()
$OracleCommand.Dispose()
$OracleConnection.Close()
"`n"
$DataSet.Tables[0] | ft | out-default