【问题标题】:BadImageFormatException of .net oracle provider.net oracle 提供者的 BadImageFormatException
【发布时间】:2016-05-08 13:13:35
【问题描述】:

我正在使用 Visual Studio 2012(64 位)和 oracle 11g(64 位)。当我去连接oracle数据库时出现以下错误。

尝试加载 Oracle 客户端库引发了 BadImageFormatException。 32位在64位模式下运行时会出现此问题 已安装 Oracle 客户端组件。

我已经花费了很多次来解决这个问题。但没有这样做。

示例代码

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");

        }
    }
}

你能帮帮我吗?

【问题讨论】:

  • 您在 Visual Studio 中为这个项目设置的平台目标是什么? (配置管理器)

标签: c# .net oracle


【解决方案1】:

Visual Studio 是 32 位应用程序,不存在 64 位版本。这取决于您的编译器设置中的目标体系结构,您需要 32 位或 64 位 Oracle 客户端。

那么,命名空间System.Data.OracleClientdeprecated 很多年了,你不应该使用它。请改用driver from Oracle

看看这个答案,我提供了一些细节: The provider is not compatible with the version of Oracle client

更多的cmets:

您应该在使用后关闭/处置OracleDataReader

使用绑定变量而不是硬编码字符串:

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;

那么您也不必关心任何ToString() 方法。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-09-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多