.net 错误图像格式异常。在安装了 32 位 Oracle 客户端组件的情况下以 64 位模式运行时会发生这种情况
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24104210/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
BadImageFormatException. This will occur when running in 64 bit mode with the 32 bit Oracle client components installed
提问by Maven
I am getting this error while on of my .Netapplication are trying to make a connection to oracle database.
在我的.Net应用程序尝试连接到 oracle 数据库时出现此错误。
The error says that This problem will occur when running in 64 bit mode with the 32 bit Oracle client components installed.. But I have made suremany times that the client installed in x64bit not 32.
错误说This problem will occur when running in 64 bit mode with the 32 bit Oracle client components installed.。但是我已经多次确定客户端安装x64在位没有32。
Date Time: 6/8/2014 10:57:55 AM: System.InvalidOperationException: 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. ---> System.BadImageFormatException: An attempt was made to load a program with an incorrect format. (Exception from HRESULT: 0x8007000B)
at System.Data.Common.UnsafeNativeMethods.OCILobCopy2(IntPtr svchp, IntPtr errhp, IntPtr dst_locp, IntPtr src_locp, UInt64 amount, UInt64 dst_offset, UInt64 src_offset)
at System.Data.OracleClient.OCI.DetermineClientVersion()
--- End of inner exception stack trace ---
at System.Data.OracleClient.OCI.DetermineClientVersion()
at System.Data.OracleClient.OracleInternalConnection.OpenOnLocalTransaction(String userName, String password, String serverName, Boolean integratedSecurity, Boolean unicode, Boolean omitOracleConnectionName)
at System.Data.OracleClient.OracleInternalConnection..ctor(OracleConnectionString connectionOptions)
at System.Data.OracleClient.OracleConnectionFactory.CreateConnection(DbConnectionOptions options, Object poolGroupProviderInfo, DbConnectionPool pool, DbConnection owningObject)
at System.Data.ProviderBase.DbConnectionFactory.CreatePooledConnection(DbConnection owningConnection, DbConnectionPool pool, DbConnectionOptions options)
at System.Data.ProviderBase.DbConnectionPool.CreateObject(DbConnection owningObject)
at System.Data.ProviderBase.DbConnectionPool.UserCreateRequest(DbConnection owningObject)
at System.Data.ProviderBase.DbConnectionPool.GetConnection(DbConnection owningObject)
at System.Data.ProviderBase.DbConnectionFactory.GetConnection(DbConnection owningConnection)
at System.Data.ProviderBase.DbConnectionClosed.OpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory)
at System.Data.OracleClient.OracleConnection.Open()
at CustomizedSetupInstaller.Runscripts.InitializeDBObjects(String connectionString, String dbProvider)
回答by Wernfried Domscheit
One solution is to install both x86 (32-bit) and x64 Oracle Clients on your machine, then it does not matter on which architecture your application is running.
一种解决方案是在您的机器上同时安装 x86(32 位)和 x64 Oracle 客户端,那么您的应用程序在哪种架构上运行就无关紧要了。
Here an instruction to install x86 and x64 Oracle client on one machine:
这是在一台机器上安装 x86 和 x64 Oracle 客户端的说明:
Assumptions: Oracle Home is called OraClient11g_home1, Client Version is 11gR2
假设:调用 Oracle Home OraClient11g_home1,客户端版本为 11gR2
Optionally remove any installed Oracle client (see How to uninstall / completely remove Oracle 11g (client)?if you face problems)
Download and install Oracle x86 Client, for example into
C:\Oracle\11.2\Client_x86Download and install Oracle x64 Client into different folder, for example to
C:\Oracle\11.2\Client_x64Open command line tool, go to folder %WINDIR%\System32, typically
C:\Windows\System32and create a symbolic linkora112to folderC:\Oracle\11.2\Client_x64(see commands section below)Change to folder %WINDIR%\SysWOW64, typically
C:\Windows\SysWOW64and create a symbolic linkora112to folderC:\Oracle\11.2\Client_x86, (see below)Modify the
PATHenvironment variable, replace all entries likeC:\Oracle\11.2\Client_x86andC:\Oracle\11.2\Client_x64byC:\Windows\System32\ora112, respective their\binsubfolder. Note:C:\Windows\SysWOW64\ora112must not be in PATH environment.If needed set your
ORACLE_HOMEenvironment variable toC:\Windows\System32\ora112Open your Registry Editor. Set Registry value
HKLM\Software\ORACLE\KEY_OraClient11g_home1\ORACLE_HOMEtoC:\Windows\System32\ora112Set Registry value
HKLM\Software\Wow6432Node\ORACLE\KEY_OraClient11g_home1\ORACLE_HOMEtoC:\Windows\System32\ora112(notC:\Windows\SysWOW64\ora112)You are done! Now you can use x86 and x64 Oracle client seamless together, i.e. an x86 application will load the x86 libraries, an x64 application loads the x64 libraries without any further modification on your system.
Probably it is a wise option to set your
TNS_ADMINenvironment variable (resp.TNS_ADMINentries in Registry) to a common location, for exampleTNS_ADMIN=C:\Oracle\Common\network.
可选择删除任何已安装的 Oracle 客户端(如果遇到问题,请参阅如何卸载/完全删除 Oracle 11g(客户端)?)
下载并安装 Oracle x86 Client,例如进入
C:\Oracle\11.2\Client_x86将 Oracle x64 Client 下载并安装到不同的文件夹中,例如
C:\Oracle\11.2\Client_x64打开命令行工具,转到文件夹 %WINDIR%\System32,通常
C:\Windows\System32并创建指向ora112文件夹的符号链接C:\Oracle\11.2\Client_x64(请参阅下面的命令部分)更改到文件夹 %WINDIR%\SysWOW64,通常
C:\Windows\SysWOW64并创建ora112到文件夹的符号链接C:\Oracle\11.2\Client_x86,(见下文)修改
PATH环境变量,替换所有像C:\Oracle\11.2\Client_x86和C:\Oracle\11.2\Client_x64by 的条目C:\Windows\System32\ora112,分别是它们的\bin子文件夹。注意:C:\Windows\SysWOW64\ora112不能在PATH环境中。如果需要,将
ORACLE_HOME环境变量设置为C:\Windows\System32\ora112打开您的注册表编辑器。将注册表值设置
HKLM\Software\ORACLE\KEY_OraClient11g_home1\ORACLE_HOME为C:\Windows\System32\ora112将注册表值设置
HKLM\Software\Wow6432Node\ORACLE\KEY_OraClient11g_home1\ORACLE_HOME为C:\Windows\System32\ora112(notC:\Windows\SysWOW64\ora112)你完成了!现在您可以无缝地使用 x86 和 x64 Oracle 客户端,即 x86 应用程序将加载 x86 库,x64 应用程序加载 x64 库而无需对您的系统进行任何进一步修改。
将
TNS_ADMIN环境变量(TNS_ADMIN注册表中的相应条目)设置为公共位置可能是一个明智的选择,例如TNS_ADMIN=C:\Oracle\Common\network.
Commands to create symbolic links:
创建符号链接的命令:
cd C:\Windows\System32
mklink /d ora112 C:\Oracle\11.2\Client_x64
cd C:\Windows\SysWOW64
mklink /d ora112 C:\Oracle\11.2\Client_x86
cd C:\Windows\System32
mklink /d ora112 C:\Oracle\11.2\Client_x64
cd C:\Windows\SysWOW64
mklink /d ora112 C:\Oracle\11.2\Client_x86
Notes:
笔记:
Both symbolic links must have the same name, e.g. ora112.
两个符号链接必须具有相同的名称,例如ora112.
Despite of their names folder C:\Windows\System32contains the x64 libraries, whereas C:\Windows\SysWOW64contains the x86 (32-bit) libraries. Don't be confused.
尽管名称不同,文件夹C:\Windows\System32包含 x64 库,而C:\Windows\SysWOW64包含 x86(32 位)库。不要混淆。
回答by DavidRR
In my situation, the Oracle 11.2 32-bit clientwasinstalled on my 64-bit Windows2008 R2 OS.
在我的情况下,甲骨文11.2 32位客户端是安装在我的64位Windows2008 R2操作系统。
My solution:In the Advanced Settings for the Application Poolassigned to my ASP.NETapplication, I set Enable 32-Bit Applicationsto True.
我的解决方案:在分配给我的ASP.NET应用程序的应用程序池的高级设置中,我将启用 32 位应用程序设置为True。
Please see below for the standalone .ashxtest script that I used to test the ability to connect to Oracle. Before making the Application Pool change, its response was:
请参阅下面的独立.ashx测试脚本,我用来测试连接到 Oracle 的能力。在更改应用程序池之前,它的响应是:
[Running as 64-bit] Connection failed.
...and after the Application Pool change:
...在应用程序池更改之后:
[Running as 32-bit] Connection succeeded.
TestOracle.ashx – Script to Test an Oracle Connection via System.Data.OracleClient:
TestOracle.ashx – 通过 System.Data.OracleClient 测试 Oracle 连接的脚本:
To use:Change the user, password and host variables as appropriate.
使用方法: 根据需要更改用户、密码和主机变量。
Note that this script can be used in a standalone fashion without disturbing your ASP.NET web application project file. Just drop it in your application folder.
请注意,此脚本可以独立使用,而不会影响您的 ASP.NET Web 应用程序项目文件。只需将其放入您的应用程序文件夹即可。
<%@ WebHandler Language="C#" Class="Handler1" %>
<%@ Assembly Name="System.Data.OracleClient, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" %>
using System;
using System.Data.OracleClient;
using System.Web;
public class Handler1 : IHttpHandler
{
private static readonly string m_User = "USER";
private static readonly string m_Password = "PASSWORD";
private static readonly string m_Host = "HOST";
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/plain";
string result = TestOracleConnection();
context.Response.Write(result);
}
public bool IsReusable
{
get { return false; }
}
private string TestOracleConnection()
{
string result = IntPtr.Size == 8 ?
"[Running as 64-bit]" : "[Running as 32-bit]";
try
{
string connString = String.Format(
"Data Source={0};Password={1};User ID={2};",
m_Host, m_User, m_Password);
OracleConnection oradb = new OracleConnection();
oradb.ConnectionString = connString;
oradb.Open();
oradb.Close();
result += " Connection succeeded.";
}
catch
{
result += " Connection failed.";
}
return result;
}
}
回答by Amir Tofighi
I had the same issue on a Windows 10 PC. I copied the project from my old computer to the new one, both 64 bits, and I installed the Oracle Client 64 bit on the new machine. I got the same error message, but after trying many solutions to no effect, what actually worked for me was this: In your Visual Studio (mine is 2017) go to Tools > Options > Projects and Solutions > Web Projects
我在 Windows 10 PC 上遇到了同样的问题。我将项目从旧计算机复制到新计算机,均为 64 位,并在新计算机上安装了 64 位 Oracle 客户端。我收到了相同的错误消息,但是在尝试了许多解决方案都没有效果之后,对我来说真正有用的是:在您的 Visual Studio(我的是 2017 年)中,转到工具 > 选项 > 项目和解决方案 > Web 项目
On that page, check the option that says: Use the 64 bit version of IIS Express for Websites and Projects
在该页面上,选中以下选项:为网站和项目使用 64 位版本的 IIS Express
回答by javier maffla
To revise IIS
修改IIS
- Select Application Pools.
- Clic in ASP .NET V4.0 Classic.
- Select Advanced Settings.
- In General, option Enable 32-Bit Applications, default is false. Select TRUE.
- Refresh and check site.
- 选择应用程序池。
- 单击 ASP .NET V4.0 Classic。
- 选择高级设置。
- 一般情况下,选项启用 32 位应用程序,默认为 false。选择真。
- 刷新并检查站点。
Comment:
评论:
Platform: Windows Server 2008 R2 Enterprise - 64Bit - IIS 7.5
平台:Windows Server 2008 R2 Enterprise - 64 位 - IIS 7.5
回答by b_levitt
As it was pointed out in the comments, System.Data.OracleClient is deprecated. There is little reason to start using it so late in the game.
正如评论中指出的那样,不推荐使用 System.Data.OracleClient。没有什么理由在游戏中这么晚才开始使用它。
Also as pointed out in the comments (I've marked this as community wiki in observence), there is now a managed provider as part of the 12c and later versions of the odp.net package. This provider does NOT require any unmanaged dlls so this should be a non issue in that case.
同样如评论中所指出的(我已将其标记为社区维基),现在有一个托管提供程序作为 odp.net 包的 12c 和更高版本的一部分。此提供程序不需要任何非托管 dll,因此在这种情况下这应该不是问题。
If you would prefer to use the old unmanaged Oracle.DataAccess provider from oracle, the simplest solution is to set the "DllPath" configuration variable:
如果您更喜欢使用来自 oracle 的旧的非托管 Oracle.DataAccess 提供程序,最简单的解决方案是设置“DllPath”配置变量:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<oracle.dataaccess.client>
<add key="DllPath" value="C:\oracle\bin"/>
</oracle.dataaccess.client>
</configuration>
See "Search Order for Unmanaged DLLs" in http://docs.oracle.com/database/121/ODPNT/InstallODP.htmfor more information
有关详细信息,请参阅http://docs.oracle.com/database/121/ODPNT/InstallODP.htm中的“非托管 DLL 的搜索顺序”
回答by theozdemir
this solution work for me ,
这个解决方案对我有用,
To revise IIS
修改IIS
Select Application Pools.
Clic in ASP .NET V4.0 Classic.
Select Advanced Settings.
In General, option Enable 32-Bit Applications, default is false. Select TRUE.
Refresh and check site.
Comment:
评论:
Platform: Windows Server 2012 Standart- 64Bit - IIS 8
平台:Windows Server 2012 标准版 - 64 位 - IIS 8
回答by gcr
I had the same problem in SSIS 2008. I tried to connect to an Oracle 11g using ODAC 12c 32 bit. Tried to install ODAC 12c 64 bit as well. SSIS was actually able to preview the table but when trying to run the package it was giving this error message. Nothing helped. Switched to VS 2013, now it was running in debug mode but got the same error when the running the package using dtexec /f filename. Then I found this page: http://sqlmag.com/comment/reply/17881.
我在 SSIS 2008 中遇到了同样的问题。我尝试使用 ODAC 12c 32 位连接到 Oracle 11g。也尝试安装 ODAC 12c 64 位。SSIS 实际上能够预览该表,但是在尝试运行该包时,它给出了此错误消息。没有任何帮助。切换到 VS 2013,现在它在调试模式下运行,但在使用 dtexec /f 文件名运行包时出现相同的错误。然后我找到了这个页面:http: //sqlmag.com/comment/reply/17881。
To make it short it says: (if the page is still there just go to the page and follow the instrucrtions...) 1) Download and install the latest version of odac 64 bit xcopy from oracle site. 2) Download and install the latest version of odac 32 bit xcopy from oracle site. How? open a cmd shell AS AN ADMINSTARTOR and run: c:\64bitODACLocation> install.bat oledb c:\odac\odac64. the first parameter is the component you want to install. The second param is where to install to. install the 32 version as well like this: c:\32bitODACLocation> install.bat oledb c:\odac\odac32. 3) Change the path of the system to include c:\odac\odac32; c:\odac\odac32\bin; c:\odac\odac64;c:\odac\odac64\bin IN THIS ORDER. 4) Restart the machine. 5) make sure you have the same tnsnames.ora in both odac32\admin\network and odac64\admin\network folders (or at least the same entry for your connection). 6) Now open up SSIS in visual studio (I used the free 2013 version with the ssis package) - Use OLEDB and then select the Oracle Provider for OLE DB provider as your connection type. Set the name of the entry in your tnsnames.ora as the "server or file name". Username is your schema name (db name) and password is the password for schema. you are done!
简而言之,它说:(如果页面仍然存在,只需转到该页面并按照说明进行操作...) 1) 从 oracle 站点下载并安装最新版本的 odac 64 位 xcopy。2) 从oracle 站点下载并安装最新版本的odac 32 位xcopy。如何?以管理员身份打开 cmd shell 并运行:c:\64bitODACLocation> install.bat oledb c:\odac\odac64。第一个参数是您要安装的组件。第二个参数是安装到哪里。像这样安装 32 版本:c:\32bitODACLocation> install.bat oledb c:\odac\odac32。3)修改系统路径,包含c:\odac\odac32;c:\odac\odac32\bin; c:\odac\odac64;c:\odac\odac64\bin 按此顺序。4) 重新启动机器。5) 确保你有相同的 tnsnames。ora 位于 odac32\admin\network 和 odac64\admin\network 文件夹中(或者至少是连接的相同条目)。6) 现在在 Visual Studio 中打开 SSIS(我使用了带有 ssis 包的免费 2013 版本) - 使用 OLEDB,然后选择 Oracle Provider for OLE DB provider 作为您的连接类型。将 tnsnames.ora 中的条目名称设置为“服务器或文件名”。用户名是您的架构名称(数据库名称),密码是架构的密码。你完成了!
Again, you can find the very detailed solution and much more in the original site.
同样,您可以在原始站点中找到非常详细的解决方案以及更多内容。
This was the only thing which worked for me and did not mess up my environment.
这是唯一对我有用并且不会弄乱我的环境的方法。
Cheers! gcr
干杯! 总站
回答by QSS
I developed desktop application using C#.net with 2.0 framework along with system.data.oracleclient for connecting oracle db and I was facing similar error message ,"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."
我使用 C#.net 和 2.0 框架以及 system.data.oracleclient 开发了用于连接 oracle db 的桌面应用程序,我遇到了类似的错误消息,“尝试加载 Oracle 客户端库抛出 BadImageFormatException。在 64 位模式下运行时会出现此问题安装了 32 位 Oracle 客户端组件。”
following solutions were applied
应用了以下解决方案
- Project, properties, Build TAB, select platform target : x86
- Project, clean build, ReBuild solution
- Install Oracle 11G*32 bit client
- 项目、属性、构建选项卡,选择平台目标:x86
- 项目、清理构建、重建解决方案
- 安装 Oracle 11G*32 位客户端
now, it work because application is set for 32bit and oracle 32bit client installed over Win2012 R2 server, hopefully will work for you.
现在,它可以工作,因为应用程序设置为 32 位,并且在 Win2012 R2 服务器上安装了 oracle 32 位客户端,希望对您有用。
回答by Santhosh
Make Enable32bit Application to TRUE in the IIS App pool which you are consuming
在您使用的 IIS 应用程序池中将 Enable32bit Application 设为 TRUE
回答by Raghu
Please download the correct version of Oracle Client like Oracle Client 11.2 32-Bit; which resolved the problem for me.
请下载正确版本的 Oracle Client,例如 Oracle Client 11.2 32-Bit;这为我解决了问题。

