Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Could not load file or assembly 'Oracle.DataAccess, Version=4.112.4.0, Culture=neutral, PublicKeyToken=89b483f429c47342'

Tags:

c#

.net

oracle

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();
                }
            }
        }
    }
}
like image 345
Marco Dinatsoli Avatar asked Dec 20 '22 05:12

Marco Dinatsoli


2 Answers

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.

like image 126
to11mtm Avatar answered May 10 '23 06:05

to11mtm


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!

like image 42
stefankmitph Avatar answered May 10 '23 06:05

stefankmitph