Oracle Connection in C# - connection string
Asked Answered
S

3

5

I'm currently trying to build an application in C# and connecting it to a live db running in Oracle 11g. I have the following connection details

Host IP: 10.204.1.3 
Port: 1521
DB Name: PROD

My source code

string connString = "DATA SOURCE=10.204.1.3:1521/PROD;PERSIST SECURITY" +
"INFO=True;USER ID=username; PASSWORD=userpass";
OracleConnection conn = new OracleConnection(connString);
conn.Open();

I was able to add a connection in Server Explorer with the Connection String used by VS but is having the error below in conn.Open();

An unhandled exception of type 'System.NullReferenceException' occurred in 
Oracle.DataAccess.dll

Sorry if this is a basic question, I'm new in VS, and Oracle and can't find the solution in the other part of the web. Thanks in advance.

Selwyn answered 25/9, 2017 at 14:46 Comment(6)
are you using a TNSNames file at all?Cutcliffe
Also, have you hand coded your connection string or have you gone through a wizard to build the connection string up and tested your connection?Cutcliffe
Add a try catch block around the open() call and resolve the ORA error that shows up in the OracleExceptionNewbill
@CodeWarrior I went through the wizard and was actually able to successfully connect to the db. The connection string I'm using is from the db properties in the server explorer.Selwyn
Your string concatenation is going to result in "PERSIST SECURITYINFO=True", which is incorrect and I believe will result in an error, if this isn't just a typo on your post. A correct connection string depends on what provider you are using and which options you are using, see connectionstrings.com/oracle.Ader
@Ader sorry, that was a typo. I've been trying to do the strings in the oracle reference since this morning, but no luck.Selwyn
S
4

My code is now working. I should've read the Oracle documentation (reference below).

string connString = "DATA SOURCE=10.204.3.1:1521/PROD;" +
"PERSIST SECURITY INFO=True;USER ID=username; password=password; Pooling 
=False;";

OracleConnection conn = new OracleConnection();
conn.ConnectionString = connString;
conn.Open();

Reference: http://www.oracle.com/webfolder/technetwork/tutorials/obe/db/12c/r1/appdev/dotnet/Web_version_Fully_Managed_ODPnet_OBE/odpnetmngdrv.html

Selwyn answered 25/9, 2017 at 16:32 Comment(0)
S
0

ConnectionString Oracle : "User Id=userName;Password=...;Data Source= localhost/mypdb"

Scutage answered 23/1 at 10:19 Comment(1)
As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.Keciakeck
M
0

I managed to connect with this string:

//using Oracle.DataAccess.Client;
    
OracleConnection con = new OracleConnection("User Id=username;Password=xxxxxxxxxx;Data Source=(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=0.0.0.0)(PORT=1521))(CONNECT_DATA=(SERVICE_NAME=host.net)));")
        
con.Open();
Mallis answered 5/7 at 7:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.