0

I am using Visual Studio 2012 (64 bit) and oracle 11g (64 bit). When I go to connect oracle database occurring following error.

Attempt to load Oracle client libraries threw BadImageFormatException. This problem will occur when running in 64 bit mode with the 32 bit Oracle client components installed.

I have spend many times to solve this problem. But did not do that.

Sample Code

using System;
using System.Data;
using System.Data.OracleClient; //Add This

namespace Oracle_database
{
    class Program
    {
        static void Main(string[] args)
        {
            OracleConnection con = new OracleConnection();

            //using connection string attributes to connect to Oracle Database
            con.ConnectionString = "User Id=abc;Password=12345;Data Source=ORCL";
            con.Open();

            DateTime fromDate = DateTime.Now.AddDays(-760);
            DateTime toDate = DateTime.Now.AddDays(-760);

            string sd = fromDate.Year + fromDate.Month.ToString("00") + fromDate.Day.ToString("00");
            string ed = toDate.Year + toDate.Month.ToString("00") + toDate.Day.ToString("00");

            var cmd = new OracleCommand();
            cmd.CommandText = String.Format("Select E.c_Date, E.c_Time, E.l_UID, E.l_TID from tEnter E where E.c_Date>='{0}' and E.c_Date<='{1}'", sd, ed);

            cmd.CommandType = CommandType.Text;
            cmd.Connection = con;
            OracleDataReader reader = cmd.ExecuteReader();

            while (reader.Read())
            {
                if (!reader.HasRows) continue;
                if (reader[2].ToString() == "-1") continue;
            }
            // Close and Dispose OracleConnection object
            con.Close();
            con.Dispose();
            Console.WriteLine("Disconnected");

        }
    }
}

Could you help me?

Shohel
  • 3,562
  • 3
  • 29
  • 66
  • whats the platform target that you have in your visual studio for this project? (configuration manager) – cableload May 08 '16 at 16:17

1 Answers1

1

Visual Studio is a 32-bit application, a 64-bit version does not exist. It depends on target architecture in your compiler settings weather you need the 32 or 64 bit Oracle Client.

Then, namespace System.Data.OracleClient is deprecated for many years, you should not use it. Use the driver from Oracle instead.

Have a look at this answer, there I provided some details: The provider is not compatible with the version of Oracle client

Some more comments:

You should close/dispose the OracleDataReader after you have use it.

Use bind-variables instead of hard-codes strings:

cmd.CommandText = "Select E.c_Date, E.c_Time, E.l_UID, E.l_TID from tEnter E where E.c_Date>= :sd and E.c_Date<= :ed";
cmd.Parameters.Add("sd", OracleDbType.Date, ParameterDirection.Input).Value = fromDate;
cmd.Parameters.Add("ed", OracleDbType.Date, ParameterDirection.Input).Value = toDate;

Then you also don't have to take care about any ToString() methods.

Community
  • 1
  • 1
Wernfried Domscheit
  • 38,841
  • 5
  • 50
  • 81