1
votes

This is my code to connect to an Oracle database:

[Reflection.Assembly]::LoadFile("E:\oracle\product\11.2.0\ODP.NET\bin\2.x\Oracle.DataAccess.dll")
$constr = "User Id=system;Password=password;Data Source=SERVER\INST"
$conn= New-Object Oracle.DataAccess.Client.OracleConnection($constr)

First line is working fine. I can see the assembly is loading and GAC is true.

Up to the second line there is no error.

But when it reaches the 3rd line, I am getting this error:

New-Object : Exception calling ".ctor" with "1" argument(s): "The type initializer for 'Oracle.DataAccess.Client.Oracle Connection' threw an exception. $conn= New-Object <<<< Oracle.DataAccess.Client.OracleConnection($constr) + CategoryInfo : InvalidOperation: (:) [New-Object], MethodInvocationException + FullyQualifiedErrorId : ConstructorInvokedThrowException,Microsoft.PowerShell.Commands.NewObjectCommand "

Can anybody advise me on this.

Powershell version is 2.

Update

PS I:\> [Reflection.Assembly]::LoadFile("E:\oracle\product\11.2.0\ODP.NET\bin\2.x\Oracle.DataAccess.dll")

GAC    Version        Location
---    -------        --------
True   v2.0.50727     C:\Windows\assembly\GAC_64\Oracle.DataAccess\2.112.3.0__89b483f429c47342\Oracle.DataAccess.dll
1

1 Answers

2
votes

Oracle.DataAccess.dll is architecture specific. That means that you have to make sure the PowerShell process and the Oracle.DataAccess.dll assembly have the same bits (32 or 64).

May I suggest you to use the platform-independent Oracle managed driver? It works a lot better than the Oracle client specific Oracle.DataAccess.

Also see my answer here.