简体   繁体   English

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

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

I have been reading about how to connect to my oracle database from my C# win application, but I keep “hitting the wall”. 我一直在阅读有关如何从C#win应用程序连接到我的oracle数据库的信息,但是我一直在“碰壁”。 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. 我决定使用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. 我有一个小型测试应用程序,下面显示了代码,在我的解决方案中,我从oracle OCI中添加了以下dll:oci.dll,orannzsbb11.dll和oraociicus11.dll。 They are all placed together with the final .exe file. 它们都与最终的.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. 该问题已经在using(…)语句中出现,程序只是停止工作,而我没有任何响应。 What is the magic that I need to do to get the OCI to work??? 我需要做些什么才能使OCI正常工作?

To be able to use ODP.NET without installing the full blown client, you need to use the Oracle Instant Client packages (you cannot just copy the libraries from a complete client): 为了能够在不安装完整客户端的情况下使用ODP.NET,您需要使用Oracle Instant Client软件包(不能仅从完整的客户端复制库):

  • Check here for a description of the requirements. 在此处查看要求的描述。
  • Starting with Oracle v10, I would strongly recommend using EZCONNECT to simplify your connection string. 从Oracle v10开始,我强烈建议您使用EZCONNECT来简化您的连接字符串。 How about this: 这个怎么样:

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

Normally when using OCI, or Oracle database products in general, the ORACLE_HOME environment variable should be defined and pointing to your oracle installation. 通常,在通常使用OCI或Oracle数据库产品时,应定义ORACLE_HOME环境变量并指向您的oracle安装。 Next to the libraries Oracle does use some other support files and it is searching for them in ORACLE_HOME. Oracle在库旁边使用其他支持文件,并且正在ORACLE_HOME中搜索它们。 Normally the LD_LIBRARY_PATH is defined as ORACLE_HOME/lib. 通常,LD_LIBRARY_PATH定义为ORACLE_HOME / lib。 Try using the Instant Client , that is most likely better than hand picking a few libs. 尝试使用Instant Client ,这比手工挑选一些库更好。 A nice article about how to get it to work is here: Installing Oracle instantclient basic and instantclient sqlplus on win32 You can leave out the part about sqlplus. 关于如何使其工作的一篇不错的文章在这里: 在win32上安装Oracle InstantClient Basic和InstantClient sqlplus您可以省略有关sqlplus的部分。

And from the instance-client page on otn: 从otn的instance-client页面:

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. 即时客户端下载请注意,即时客户端是根据即时客户端的单独OTN开发和分发许可证提供的,该许可证允许大多数被许可方免费下载,重新分发和在生产环境中进行部署。 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网站。

It looks like you are allowed to redistribute the instance-client. 看起来您被允许重新分配实例客户端。

Read the installation instructions from Oracle for ODAC found here . 阅读Oracle的ODAC安装说明,位于此处 This also discusses common setup issues. 这也讨论了常见的设置问题。

You'll also need to include a reference to Oracle.DataAccess in your solution and "using Oracle.DataAccess.Client;" 您还需要在解决方案中以及“使用Oracle.DataAccess.Client;”中包含对Oracle.DataAccess的引用。 For your connection, you may want to use an Oracle SID that can be resolved via your tnsnames file (try tnsping from cmd prompt). 对于您的连接,您可能希望使用可以通过tnsnames文件解析的Oracle SID(尝试从cmd提示符下tnsping)。

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

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