Error connecting Oracle with c#

programinca 21 Reputation points
2021-10-19T12:51:15.877+00:00

Hi everyone. I have an issue trying to connect Oracle with C#, I hope you can helpme.
I’m using Oracle SQL Developer and make the connection successfully:

141774-f1.png

Everything Works fine:

141755-f2.png

But then, when I’m trying to connect to that database from C#, I get an error:

using System.Data.OracleClient;  
  
DataTable dtResult = new DataTable();              
              
string cnnString2 = @"Data Source=xe; USER ID=system; PASSWORD=admin;";  
  
using (OracleConnection cnn = new OracleConnection(cnnString2))  
{  
    OracleCommand cmd = new OracleCommand( @"SELECT * FROM TABLE");  
  
    cmd.CommandType = CommandType.Text;  
  
    OracleDataAdapter da = new OracleDataAdapter(cmd);  
  
    cnn.Open();  
  
    da.Fill(dtResult);  
  
    cnn.Close();              
}  
  
return dtResult;  

In the cnn.Open line, I get this error:

System.Data.OracleClient.OracleException: 'ORA-12154: TNS:could not resolve the connect identifier specified

I tried this same code in my personal laptop, and works ok, but I’m using now a Laptop from my company, and I get that error. The strange thing is that I can connect from the soft Oracle SQL Developer, but from C# nope. Anyone have some solution for this?

Thanks in advance.

Azure Database Migration service
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
11,006 questions
VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,735 questions
0 comments No comments
{count} votes

Accepted answer
  1. Karen Payne MVP 35,436 Reputation points
    2021-10-19T14:54:50.737+00:00

    Try Persist Security Info=True;

    Data Source=TODO;Persist Security Info=True;Enlist=false;Pooling=true;Statement Cache Size=10;User ID=TODO;Password=TODO;

    This comes from porting a connection string from SQL-Developer (or Toad)

    141843-figure1.png

    1 person found this answer helpful.
    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.