5

I have installed oracle 11 g r 2 in the server, and I downloaded ODAC112040Xcopy_64bit and installed the .net components.

I copied Oracle.DataAccess.dll that exists in this location Oracle_Folder\odp.net\bin\4 to the bin folder inside my Visual Studio project

When I executed my code, I got this exception:

An unhandled exception of type 'System.BadImageFormatException' occurred in TestOracleConnection.exe

Additional information: Could not load file or assembly 'Oracle.DataAccess, Version=4.112.4.0, Culture=neutral, PublicKeyToken=89b483f429c47342' or one of its dependencies. An attempt was made to load a program with an incorrect format.

My code is:

public string CallCardDetails(string CallCardNo)
{
    //initialize
    using (DataSet ds = new DataSet())
    {
        //connect
        using (OracleConnection conn = new OracleConnection("User Id=oraDB;Password=ora;Data Source=CCT"))
        {
            // Oracle uses : for parameters, not @
            string query = "SELECT idcard from CallCardTable where idcard= :pCallCardNo";

            // Let the using block dispose of your OracleCommand
            using (OracleCommand cmd = new OracleCommand(query, conn))
            {
                // Note: be careful with AddWithValue: if there's a mismatch between the .NET datatype of
                // CallCardNo and the idcard column you could have an issue.  Cast the value you provide
                // here to whatever is closest to your database type (String for VARCHAR2, DateTime for DATE, Decimal for NUMBER, etc.)
                cmd.Parameters.Add(":pCallCardNo", CallCardNo);
                conn.Open();

                // Again, wrap disposables in a using or use try/catch/finally (using will dispose of things in case of exceptions too)
                using (OracleDataAdapter dA = new OracleDataAdapter(cmd))
                {
                    dA.Fill(ds);

                    return ds.GetXml();
                }
            }
        }
    }
}
shA.t
  • 16,580
  • 5
  • 54
  • 111
Marco Dinatsoli
  • 10,322
  • 37
  • 139
  • 253

6 Answers6

8

Okay, I'm going to suggest the following based on my experience with ODP.NET:

Your system is attempting to load the 64 bit Oracle DLL, and can't because the application is running in 32 bit mode. Try setting your application to explicitly be 64 Bit. Alternatively, install the 32 bit ODP.Net drivers and see if those work any better.

to11mtm
  • 169
  • 6
3

Had a similar problem some time ago... Referring to this question here:

Try to set your projects 'Platform target' to 'x86' and not 'Any CPU'.

Hope this helps!

Community
  • 1
  • 1
stefankmitph
  • 3,236
  • 2
  • 18
  • 22
2

This is how I resolved it:

Install-Package ODP.NET4 -Version 4.112.3

Pholoso Mams
  • 467
  • 1
  • 5
  • 19
1

Check that target framework of the dll and your project match.The file location you posted shows you took it from Oracle_Folder\odp.net\bin\4 the bin folder for 4 version. Look if there is the req dll in bin\4.5 that maybe what you need. As this exception can also occur if you add dlls to your project whose target framework do not match.
Also, clean your solution before placing the dll in bin folder. :)

Syed Osama Maruf
  • 1,895
  • 2
  • 20
  • 37
  • i changed the project framework to `4` and i am still having the exact same problem – Marco Dinatsoli Mar 29 '15 at 18:54
  • @Marco Dinatsoli download ODP.NET_Managed121020.zip - 2.53 MB (2,662,823 bytes) from http://www.oracle.com/technetwork/database/windows/downloads/index-090165.html then from there use odp.net/managed/common/Oracle.ManagedDataAccess.dll – Syed Osama Maruf Mar 30 '15 at 15:07
  • Also donot manually place the dll in the bin. Add ref of it in your proj and make sure the copy local property is set to true. Rebuild the project and run. – Syed Osama Maruf Mar 30 '15 at 15:29
1

I usually use the NuGet packages for ODP.net, they work fine. You can find them here.

This is all you need to build your solution, you do not need to install any drivers.

Tshilidzi Mudau
  • 7,373
  • 6
  • 36
  • 49
Maarten
  • 22,527
  • 3
  • 47
  • 68
0

In order to use the Oracle Data Prover for .NET (ODP.NET) Version 4.112.4.0 64bit following conditions must apply:

  • Target Framework is 4.0 or higher
  • Architecture must be x64 or AnyCPU (in case of an 64bit Windows, which is most likely the case)
  • An 64-Bit Oracle Client version 11.2 is installed on your PC and on the target machine

Find some more information in this anser: The provider is not compatible with the version of Oracle client

Community
  • 1
  • 1
Wernfried Domscheit
  • 54,457
  • 9
  • 76
  • 110