使用 odp.net 和 C# 中的 OCI 连接到 Oracle

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5647137/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 23:19:44  来源:igfitidea点击:

Connect to Oracle with odp.net and the OCI from C#

c#oracle.net-4.0odp.netoci

提问by aweis

I have been reading about how to connect to my oracle database from my C# win application, but I keep “hitting the wall”. I have decided to use odp.net and OCI, such that the client computer not needs to install a client, but I can't get it to work.

我一直在阅读有关如何从我的 C# win 应用程序连接到我的 oracle 数据库的信息,但我一直在“碰壁”。我决定使用 odp.net 和 OCI,这样客户端计算机就不需要安装客户端,但我无法让它工作。

I have a small test application, the code I shown below and in my solution I have added the following dll's from oracle OCI: oci.dll, orannzsbb11.dll and oraociicus11.dll. They are all placed together with the final .exe file.

我有一个小型测试应用程序,我在下面显示的代码和我的解决方案中,我从 oracle OCI 添加了以下 dll:oci.dll、orannzsbb11.dll 和 oraociicus11.dll。它们都与最终的 .exe 文件放在一起。

Test Code:

测试代码:

private static string CONNECTION_STRING =
                  "User Id=hr;Password=hr;Data Source=(DESCRIPTION=" +
                  "(ADDRESS=(PROTOCOL=TCP)(HOST=127.0.0.1)(PORT=1521))" +
                  "(CONNECT_DATA=(SID=XE)));Connect Timeout=15;";

        static void Main(string[] args)
        {
            try
            {
                using (var conn = new OracleConnection(CONNECTION_STRING))
                {
                    conn.Open();
                    Console.WriteLine("Connection is: {0}", conn.State.ToString());
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }

The problem occurs already in the using(…) statement, the program just stop working and I get no response. What is the magic that I need to do to get the OCI to work???

问题已经出现在 using(...) 语句中,程序只是停止工作,我没有得到任何回应。我需要做什么才能让 OCI 工作?

采纳答案by Mac

To be able to use ODP.NET without installing the full blown client, you need to use the Oracle Instant Clientpackages (you cannot just copy the libraries from a complete client):

为了能够在不安装完整客户端的情况下使用 ODP.NET,您需要使用Oracle Instant Client包(您不能只从完整客户端复制库):

  • Check herefor a description of the requirements.
  • Starting with Oracle v10, I would strongly recommend using EZCONNECTto simplify your connection string. How about this:

    private const string CONNECTION_STRING="User Id=hr;Password=hr;"+
       +"Data Source=127.0.0.1:1521/XE;Connect Timeout=15;";
    
  • 在此处查看要求的说明。
  • 从 Oracle v10 开始,我强烈建议使用EZCONNECT来简化您的连接字符串。这个怎么样:

    private const string CONNECTION_STRING="User Id=hr;Password=hr;"+
       +"Data Source=127.0.0.1:1521/XE;Connect Timeout=15;";
    

回答by ik_zelf

Normally when using OCI, or Oracle database products in general, the ORACLE_HOME environment variable should be defined and pointing to your oracle installation. Next to the libraries Oracle does use some other support files and it is searching for them in ORACLE_HOME. Normally the LD_LIBRARY_PATH is defined as ORACLE_HOME/lib. Try using the Instant Client, that is most likely better than hand picking a few libs. A nice article about how to get it to work is here: Installing Oracle instantclient basic and instantclient sqlplus on win32You can leave out the part about sqlplus.

通常在使用 OCI 或一般的 Oracle 数据库产品时,应定义 ORACLE_HOME 环境变量并指向您的 oracle 安装。在库旁边,Oracle 确实使用了一些其他支持文件,并且它正在 ORACLE_HOME 中搜索它们。通常 LD_LIBRARY_PATH 定义为 ORACLE_HOME/lib。尝试使用Instant Client,这很可能比手动选择几个库更好。一篇关于如何让它工作的好文章在这里:在 win32 上安装 Oracle Instantclient basic 和 instantclient sqlplus您可以省略关于 sqlplus 的部分。

And from the instance-client page on otn:

从 otn 上的实例客户端页面:

Instant Client Downloads Please note that Instant Client is provided under a separate OTN Development and Distribution License for Instant Client that allows most licensees to download, redistribute, and deploy in production environments, without charge. Please consult the license and your legal department for clarification, if necessary. For more information on Instant Client, see the official Instant Client site.

Instant Client 下载 请注意,Instant Client 是根据 Instant Client 的单独 OTN 开发和分发许可证提供的,该许可证允许大多数被许可方在生产环境中免费下载、重新分发和部署。如有必要,请咨询许可证和您的法律部门以进行澄清。有关 Instant Client 的更多信息,请参阅官方 Instant Client 站点。

It looks like you are allowed to redistribute the instance-client.

看起来您可以重新分发实例客户端。

回答by tbone

Read the installation instructions from Oracle for ODAC found here. This also discusses common setup issues.

此处阅读 Oracle for ODAC 的安装说明。这也讨论了常见的设置问题。

You'll also need to include a reference to Oracle.DataAccess in your solution and "using Oracle.DataAccess.Client;" For your connection, you may want to use an Oracle SID that can be resolved via your tnsnames file (try tnsping from cmd prompt).

您还需要在您的解决方案和“使用 Oracle.DataAccess.Client;”中包含对 Oracle.DataAccess 的引用。对于您的连接,您可能希望使用可通过 tnsnames 文件解析的 Oracle SID(尝试从 cmd 提示符下使用 tnsping)。