1

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
Roxx
  • 3,738
  • 20
  • 92
  • 155

1 Answers1

2

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.

Community
  • 1
  • 1
Patrick Hofman
  • 153,850
  • 22
  • 249
  • 325
  • I can't install anything on the server. Due to approval from other teams. GAC is showing True that means Oracle.DataAccess.dll is loading properly am i correct? On the server there are two version of Oracle.DataAccess.dll is available one is 2.x and another one is 4. I can load 2.x properly but i am not able to load 4. – Roxx Aug 11 '15 at 08:29
  • Use the 2.0 version. I guess you have to install another client version to get it working then. Or use the managed version which is just copy/paste. – Patrick Hofman Aug 11 '15 at 08:37
  • How would i know which is managed version. I am not a expert of Oracle – Roxx Aug 11 '15 at 09:06
  • There is just one. Use the latest version. I have tested it to work on all recent versions. – Patrick Hofman Aug 11 '15 at 09:07
  • So, what does that help? Did you try the managed driver? The only thing you can read is that it loads the 64-bits driver. I guess your process is 32-bit? – Patrick Hofman Aug 11 '15 at 09:57
  • Machine is 64-bit. I checked that. – Roxx Aug 11 '15 at 10:01
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/86684/discussion-between-404-and-patrick-hofman). – Roxx Aug 11 '15 at 10:03