简体   繁体   English

如何使用ODAC C#连接到Oracle数据库

[英]How to connect to oracle database with ODAC c#

I am using this code but getting an error of 'Object reference not set to an instance of an object.' 我正在使用此代码,但收到“对象引用未设置为对象的实例”的错误。 at con.open() ? 在con.open()? what am I doing wrong ? 我究竟做错了什么 ?

I have already download and installed ODAC component version 10 , 11 ,12 trying each one at the failure of the latest one but still same error 我已经下载并安装了ODAC组件版本10、11、12,尝试在最新的ODAC组件版本失败的情况下尝试每个组件,但仍然出现相同的错误

using Oracle.DataAccess.Client;


namespace WindowsFormsApplication1
{
    class OraTest
    {

        public OracleConnection con = new OracleConnection();
        public void Connect()
        {
            con.ConnectionString = "Data Source=(DESCRIPTION= (ADDRESS = (PROTOCOL = TCP)(HOST =myip) (PORT = myport))(CONNECT_DATA = (SERVER = dedicated)(SERVICE_NAME = mydb)));User ID=myid;Password=mypass;";

            con.Open(); //error here

        }

        public void Close()
        {
            con.Close();
            con.Dispose();
        }


    }

If you add a try/catch block in Connect(), you'll be able to catch the error. 如果在Connect()中添加try / catch块,则可以捕获该错误。

For example: 例如:

When opening an oracle connection, connection object is null 打开oracle连接时,连接对象为null

I added the try catch block, and it returned ORA12154 - TNS could not be resolved. 我添加了try catch块,它返回了ORA12154-TNS无法解析。 After some research, I added an SID to my tnsnames.ora file in my ODP for .NET Oracle home path, and it worked 经过一番研究,我在ODP for .NET Oracle主目录的tnsnames.ora文件中添加了一个SID,它可以正常工作

See also Troubleshooting Oracle Net Services for troubleshooting possible connection issues from Oracle clients (such as your C# program). 另请参阅对Oracle Net Services进行故障排除,以对来自Oracle客户端(例如C#程序)的可能的连接问题进行故障排除。

But your first step is absolutely to determine the Oracle-level error (for example, ORA-12543 (could not connect to server host) or TNS-12514 (could not find service name) 但是,您的第一步绝对是确定Oracle级别的错误(例如,ORA-12543(无法连接到服务器主机)或TNS-12514(找不到服务名称)

MSDN: OracleException Class MSDN:OracleException类

public void ShowOracleException() 
{
   OracleConnection myConnection =
      new OracleConnection("Data Source=Oracle8i;Integrated Security=yes");

   try 
   {
      myConnection.Open();
   }
   catch (OracleException e) 
   {
     string errorMessage = "Code: " + e.Code + "\n" +
                           "Message: " + e.Message;

     System.Diagnostics.EventLog log = new System.Diagnostics.EventLog();
     log.Source = "My Application";
     log.WriteEntry(errorMessage);
     Console.WriteLine("An exception occurred. Please contact your system administrator.");
   }
}

It's significant that con.ConnectionString = xyz works, but the following `con.Open()" fails. This means .Net is creating the C# object, but Oracle/TNS is failing when you try to use it. con.ConnectionString = xyz起作用很重要,但是以下的“ con.Open()”失败了,这意味着.Net正在创建C#对象,但是当您尝试使用它时,Oracle / TNS却失败了。

ADDITIONAL SUGGESTIONS: 其他建议:

Please go through this link 请通过此链接

Getting Started with Oracle Data Provider for .NET (C# Version) Oracle Data Provider for .NET入门(C#版)

http://www.oracle.com/webfolder/technetwork/tutorials/obe/db/dotnet/GettingStartedNETVersion/GettingStartedNETVersion.htm http://www.oracle.com/webfolder/technetwork/tutorials/obe/db/dotnet/GettingStartedNETVersion/GettingStartedNETVersion.htm

What worked for me with the same error was to simply switch from the 'plain' Oracle DataAccess library, to the 'Managed' version. 对于我来说,出现相同错误的原因是,只需从“普通” Oracle DataAccess库切换到“托管”版本即可。 This is an extemely easy change to make - 这是一项非常容易的更改-

  1. Add a Reference in your c# project to the Oracle.ManagedDataAccess library 在c#项目Oracle.ManagedDataAccess引用添加到Oracle.ManagedDataAccess
  2. Replace the existing use statements at the top of your Oracle client code with the following: 将Oracle客户端代码顶部的现有use语句替换为以下内容:

     using Oracle.ManagedDataAccess.Client; using Oracle.ManagedDataAccess.Types; 
  3. Include the Oracle.ManagedDataAccess.dll file with your exe 包含exe的Oracle.ManagedDataAccess.dll文件

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

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