【问题标题】:Checking if Oracle.DataAccess Exists检查 Oracle.DataAccess 是否存在
【发布时间】:2012-01-22 01:52:51
【问题描述】:

我想检查是否安装了正确版本的 Oracle 驱动程序并且可以在应用程序运行之前找到,以便我可以显示错误消息并正常失败。这是一个 C# windows 窗体应用程序。

当我在没有正确版本的 Oracle 的机器上运行应用程序时,我收到以下消息并且应用程序处于挂起状态:

Could not load file or assembly 'Oracle.DataAccess, Version=2.112.1.0, Culture=neutral, PublicKeyToken=89b483f429c47342' or one of its dependencies. The system cannot find the file specified.

如何可靠地检查此程序集是否可用?

【问题讨论】:

  • 也许我今天对类似问题的回答会有所帮助:stackoverflow.com/questions/8531475/…
  • 我需要在运行时以编程方式检查它是否存在,而不是检查它是否已安装。我在想 .NET 中应该有一种简单的方法可以做到这一点,但我就是找不到。
  • 想通了。尝试过...赶上:Assembly.Load("Oracle.DataAccess, Version=2.112.1.0, Culture=neutral, PublicKeyToken=89b483f429c47342");

标签: c# .net winforms oracle oracle11g


【解决方案1】:

您可以使用 System.Data.Common.DbProviderFactories 来实现相同的目的。 下面的示例代码可以为您检查。

    private bool checkSpecifiedProviderExists()
    {
        var connectionStringSettings = ConfigurationManager.ConnectionStrings["YourConnectionString"];  
        var factory = DbProviderFactories.GetFactory(connectionStringSettings.ProviderName);    

        try
        {
            var dbConnection = factory.CreateConnection();
            if(dbConnection !=null) return true;
                return false;
        }
        catch
        {
                return false;
        }
    }

【讨论】:

    【解决方案2】:

    除了您自己的答案之外,您还可以使用AssemblyResolve 事件以一般方式(针对所有程序集加载问题)执行此操作。这是一个例子:

    AppDomain.CurrentDomain.AssemblyResolve += (sender, prms) => {
        Console.WriteLine("Could not load assembly \"{0}\".", prms.Name);
        Console.ReadLine();
        Environment.Exit(1);
    
        return null;
    };
    
    Assembly.Load("this asembly does not exist");
    

    【讨论】:

      猜你喜欢
      • 2015-10-18
      • 2018-09-10
      • 2011-05-09
      • 2012-10-15
      • 2018-08-22
      • 2013-01-12
      • 2013-06-18
      • 2012-01-17
      • 2012-05-17
      相关资源
      最近更新 更多