无法加载文件或程序集'Oracle.DataAccess,Version = 4.112.4.0,Culture = neutral,PublicKeyToken = 89b483f429c47342'

Mar*_*oli 3 .net c# oracle

我在服务器上安装了oracle 11 gr 2,并下载ODAC112040Xcopy_64bit并安装了.net组件.

Oracle.DataAccess.dll将此位置中存在的内容 复制Oracle_Folder\odp.net\bin\4binVisual Studio项目中的文件夹中

当我执行我的代码时,我遇到了这个异常:

TestOracleConnection.exe中发生了未处理的"System.BadImageFormatException"类型异常

附加信息:无法加载文件或程序集"Oracle.DataAccess,Version = 4.112.4.0,Culture = neutral,PublicKeyToken = 89b483f429c47342"或其依赖项之一.尝试加载格式不正确的程序.

我的代码是:

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();
                }
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

小智 5

好的,我将基于我对ODP.NET的经验建议以下内容:

您的系统正在尝试加载64位Oracle DLL,但由于应用程序在32位模式下运行,因此无法加载.尝试将应用程序设置为64位.或者,安装32位ODP.Net驱动程序,看看它们是否更好.


ste*_*tph 3

前段时间有过类似的问题......在这里参考这个问题:

尝试将项目的“平台目标”设置为“x86”,而不是“任何 CPU”。

希望这可以帮助!

  • “x86”应用程序无法加载 64 位 ODP.NET 提供程序(已安装)。 (3认同)