[英]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: 其他建议:
Re-read 重读
When opening an oracle connection, connection object is null . 打开oracle连接时,连接对象为null 。
Read all of the suggestions, including the one about "Data Source in your connection string". 阅读所有建议,包括有关“连接字符串中的数据源”的建议。
Focus on your connection string. 专注于您的连接字符串。 It couldn't hurt to specify the connection string in your OracleConnection() constructor, if possible.
如果可能的话,在您的OracleConnection()构造函数中指定连接字符串也可以。 Here's another link:
这是另一个链接:
It would be great if you can verify connectivity from your PC with some other Oracle client, besides your C#/.Net program. 如果您可以验证PC与C#/。Net程序以外的其他 Oracle客户端之间的连接,那就太好了。 To verify you're talking to the right TNS host and service, with the correct username/password.
为了验证您正在使用正确的用户名/密码与正确的TNS主机和服务进行对话。 For example, maybe you have SQLDeveloper or sqlplus.
例如,也许您有SQLDeveloper或sqlplus。
Finally, re-read the TNS troubleshooting link: 最后,重新阅读TNS故障排除链接:
https://docs.oracle.com/cd/E11882_01/network.112/e41945/trouble.htm#NETAG016 https://docs.oracle.com/cd/E11882_01/network.112/e41945/trouble.htm#NETAG016
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 -
这是一项非常容易的更改-
Oracle.ManagedDataAccess
library Oracle.ManagedDataAccess
引用添加到Oracle.ManagedDataAccess
库 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;
Include the Oracle.ManagedDataAccess.dll
file with your exe 包含exe的
Oracle.ManagedDataAccess.dll
文件
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.