让我们做一个总结:
错误信息"The provider is not compatible with the version of Oracle client"可能有多个原因导致。同时,错误信息可能有不同的类型,例如"Object reference not set to an instance of an object."或"Could not load file or assembly 'Oracle.DataAccess,' or one of its dependencies"
- 你没有安装Oracle客户端。在这种情况下,错误信息确实会误导。
Oracle Data Provider for .NET (ODP.NET,即文件Oracle.DataAccess.dll
)未包含在Oracle Instant Client中,必须单独安装(从32位 Oracle 数据库访问组件(ODAC)或64位 Oracle 数据库访问组件(ODAC)下载),或者您必须在Oracle Universal Installer(OUI)中选择相应选项。
请注意,在安装Oracle Data Provider>=12.1时,提供程序不会自动注册到GAC中。如果需要,请手动注册,参见Oracle Doc 2272241.1。
ODP.NET的版本与安装的Oracle Client版本不匹配。您必须检查甚至是小版本号!例如,Oracle.DataAccess.dll
版本4.112.3.0与Oracle Client11.2.0.4不兼容。仔细检查ODP.NET和Oracle Client的版本。您可以在oraociei*.dll
和/或OraOps*w.dll
上使用sigcheck来获取Oracle Client的版本。
请注意不同的编号方案。文件版本4.112.3.0表示:.NET Framework Version 4, Oracle Release 11.2.0.3.x。
有ODP.NET版本“1.x”,“2.x”和“4.x”。这些数字与Microsoft .NET Framework版本1.0.3705/1.1.4322、2.0.50727和4.0.30319相关。版本“1.x”可用直到Oracle Client 11.1。版本“4.x”从Oracle Client 11.2开始引入
ODP.NET的架构(32位或64位)与您的应用程序架构不匹配。 32位应用程序仅适用于32位Oracle Client / ODP.NET,而64位应用程序则需要64位Oracle Client / ODP.NET。(除非您使用ODP.NET Managed Driver)
.NET Framework版本不匹配。例如,如果您使用Target .NET Framework 2.0编译应用程序,则无法使用ODP.NET版本4.x。.NET Framework目标版本必须等于或高于ODP.NET版本。
在您的开发计算机上(即编译时加载的版本),Oracle.DataAccess.dll
的版本高于目标计算机上的版本。
请注意,Oracle.DataAccess.dll
可能会从GAC加载,该加载方式默认优先于任何本地提供的文件。
新版的ODP.NET需要更高版本的Microsoft .NET Framework。例如,ODP.NET版本21.4需要.NET Framework 4.8。请检查您发布的 "Data Provider for .NET Developer's Guide"中的系统要求。
解决方案
考虑使用ODP.NET Managed Driver,可以从Oracle页面下载:64-bit Oracle Data Access Components (ODAC) Downloads。在那里,您只需将Oracle.ManagedDataAccess.dll
文件复制到应用程序目录中,无需其他操作即可使用。它适用于32位和64位。
在您的*.csproj
,或*.vbproj
中,编辑对ODP.NET的引用,如下所示:
<Reference Include="Oracle.DataAccess">
<SpecificVersion>False</SpecificVersion>
<Private>False</Private>
</Reference>
Attributes like Version=...
or processorArchitecture=...
are not required. It is expected that the correct Oracle.DataAccess.dll
will be loaded for your application based on the selected architecture and target .NET framework, assuming it has been installed properly (though this is not 100% verified).
If you are unsure of the version of Oracle Client on the target machine (e.g. if it belongs to a customer), you can download the least XCopy version of Oracle Data Access Components from the aforementioned download page. Extract the zip file and copy only the Oracle.DataAccess.dll
file to your local machine. In your VS project, make a reference to this DLL (which may be outdated), and note that the version of this DLL is the least version of ODP.NET that your application will work with. When you run your application, the Publisher Policy in GAC will redirect to the actually installed version.
It is not wise to take individual DLLs and copy them to specific folders, as there is a high risk of version mismatch if the target machine has any installed Oracle products. To avoid this issue, it is recommended to uninstall all Oracle products from your machine and perform a fresh installation. Refer to How to uninstall / completely remove Oracle 11g (client)? to ensure that your machine is truly clean.
If you need to work with both 32-bit and 64-bit applications simultaneously, follow these steps to install both versions on one machine:
Assumptions: Oracle Home is called OraClient11g_home1
, Client Version is 11gR2.
Optionally remove any installed Oracle client
Download and install the Oracle x86 Client into a folder such as C:\Oracle\11.2\Client_x86
.
Download and install the Oracle x64 Client into a different folder, such as C:\Oracle\11.2\Client_x64
.
Open a command line tool, navigate to the %WINDIR%\System32 folder (usually C:\Windows\System32
), and create a symbolic link named ora112
that points to the C:\Oracle\11.2\Client_x64
folder (see below).
Navigate to the %WINDIR%\SysWOW64 folder (usually C:\Windows\SysWOW64
) and create a symbolic link named ora112
that points to the C:\Oracle\11.2\Client_x86
folder (see below).
修改 PATH
环境变量,将所有类似于 C:\Oracle\11.2\Client_x86
和 C:\Oracle\11.2\Client_x64
的条目替换为 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
(而不是 C:\Windows\SysWOW64\ora112
)。
完成!现在你可以无缝地使用 x86 和 x64 Oracle 客户端,即一个 x86 应用程序将加载 x86 库,一个 x64 应用程序将加载 x64 库,无需对你的系统进行任何其他修改。
创建符号链接的命令:
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
注意事项:
两个符号链接的名称必须相同,例如ora112
。
如果您想手动安装ODP.NET,请注意选择适当的安装文件夹。
尽管名称如此,文件夹C:\Windows\System32
包含x64库,而C:\Windows\SysWOW64
包含x86(32位)库。不要混淆。
也许将您的TNS_ADMIN
环境变量(或注册表中的TNS_ADMIN
条目)设置为一个公共位置是个明智的选择,例如 TNS_ADMIN=C:\Oracle\Common\network
。
Oracle.DataAccess.dll
已经被安装在GAC中。在这种情况下,Oracle.DataAccess.dll
将从 GAC 中获取(优先于.exe文件夹),但其他DLL文件将从.exe文件夹中获取。这可能会导致“版本不兼容”的错误。 - Wernfried Domscheit