简体   繁体   English

在Windows 10 C#中为ODP.NET加载DLL时出错

[英]Error loading dll for ODP.NET in Windows 10 C#

I am running: 我在跑步:

  1. Windows 10 Home Windows 10家庭版
  2. Oracle Database 11g Enterprise Edition 11.2.0.1 - 64 bit version Oracle Database 11g企业版11.2.0.1-64位版本
  3. Visual Studio Express 2013 Visual Studio速成版2013

I want to write a simple C# application to read data from the Oracle database and want to use ODP.NET. 我想编写一个简单的C#应用​​程序以从Oracle数据库读取数据,并想使用ODP.NET。 However, when running the program I'm getting an immediate Runtime Error. 但是,在运行程序时,我会立即遇到运行时错误。

  1. I started a new Windows Forms project. 我开始了一个新的Windows Forms项目。

  2. I linked service Oracle.DataAccess.dll in directory c:\\app\\Dave\\product\\11.2.0\\dbhome_1\\ODP.NET\\bin\\2.x\\ 我在目录c:\\ app \\ Dave \\ product \\ 11.2.0 \\ dbhome_1 \\ ODP.NET \\ bin \\ 2.x \\中链接了服务Oracle.DataAccess.dll

My code is: 我的代码是:

using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace testapp_2
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());
        }
    }
}

Code : 代码:

using Oracle.DataAccess.Client;
using Oracle.DataAccess.Types;  

namespace testapp_2
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            OracleConnection conn = new OracleConnection();
        }
    }
}

The error is: 错误是:

Could not load file or assembly 'Oracle.DataAccess, Version=2.112.1.0, Culture=neutral, PublicKeyToken=89b483f429c47342' or one of its dependencies. 无法加载文件或程序集'Oracle.DataAccess,Version = 2.112.1.0,Culture = neutral,PublicKeyToken = 89b483f429c47342'或其依赖项之一。 An attempt was made to load a program with an incorrect format. 试图加载格式错误的程序。

When running in debug mode, the program halts at line: 在调试模式下运行时,程序在以下行停止:

Application.Run(new Form1());

I have used this in the past successfully with Visual Stdio Express 2008 and Oracle 10g under Windows XP and thought this would be very simple just like before, but apparently not. 过去,我已经在Windows XP下成功地在Visual Stdio Express 2008和Oracle 10g中成功使用了此功能,并认为这将像以前一样非常简单,但显然不是。

Has anyone else encountered this error and hopefully have found a workaround for it? 其他人是否遇到过此错误,并希望找到解决方法? I would be most appreciative of any help or information. 对于任何帮助或信息,我将不胜感激。

Thank you very much! 非常感谢你!

-Dave. 戴夫。

In some cases where .net DLL is not targeting both x86 and x64 platform (like Microsoft Expression Encoder) under 64-bit system. 在某些情况下,.net DLL在64位系统下不能同时面向x86和x64平台(例如Microsoft Expression Encoder)。 this error might occur when launching debugging (not sure it's the same case for ODP.NET). 启动调试时可能会发生此错误(不确定ODP.NET是否相同)。 The solution is to change your build settings in the project to target x86 instead of Any CPU. 解决方案是将项目中的构建设置更改为x86而不是Any CPU。

Thanks to everyone for posting so many helpful comments. 感谢大家发表这么多有用的评论。

I tried following the suggestion to change the target CPU to x86 instead of Any CPU (I also tried x64), but it made no difference. 我尝试按照建议将目标CPU更改为x86而不是Any CPU(我也尝试过x64),但没有区别。

But thinking about that topic got me thinking that perhaps this is an old ODP.NET dll, and isn't compatible with the current/latest versions of .NET. 但是考虑到该主题后,我想到了这可能是旧的ODP.NET dll,并且与.NET的当前/最新版本不兼容。

So (in Properties and the Application tab, I changed the target .NET framework to be .NET 3.5 and sure enough that solved my problems! 因此(在“属性”和“应用程序”选项卡中,我将目标.NET框架更改为.NET 3.5,并且可以解决我的问题!

I'm now able to read and write data to and from the database from C#! 现在,我可以从C#向数据库读写数据了! I'm very happy and relieved that it is now working. 我很高兴并为它现在正在工作感到欣慰。

Thanks again to everyone for their assistance in leading me to solving this issue! 再次感谢大家为我提供的帮助,以解决这个问题! :-) :-)

-D. -D。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM