9

When I open SQL Command Line, I write CONNECT username/password@[//]host[:port][/service_name] and it connects me to the database just fine. However, I'm unable to connect from a .NET project using a connection string. I have tried many things such as <add name="ConnectionString" connectionString="Data Source=username/password@[//]host[:port][/service_name];" /> and <add name="ConnectionString" connectionString="server=tcp:host;Initial Catalog=service_name; user id=username; password=password; Connection Timeout=180;" providerName="System.Data.OracleClient" />, but nothing worked so far. Whenever I get to sconn.Open(); in the following:

var CurrentConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
SqlConnection sconn = new SqlConnection(CurrentConnectionString);
sconn.Open();

I practically always get the following error:

A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: SQL Network Interfaces, error: 25 - Connection string is not valid)

How can I connect to the database properly?

Mathieu Roy
  • 93
  • 1
  • 1
  • 3
  • 1
    http://www.connectionstrings.com/oracle/ – Ňɏssa Pøngjǣrdenlarp Mar 04 '16 at 02:34
  • The server could be configured to not allow remote connections to. That's a thing in all the main RDBMS servers, MSSQL, PostgreSQL, MySql, and I imagine Oracle to. If it is not allowing remote connections then it will not be reachable even when your connection string is correct. – Ryan Mann Mar 04 '16 at 02:51

4 Answers4

10

Try following connection string

string con = "Data Source=(DESCRIPTION =(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST = 000.00.0.00)(PORT = 0000)))(CONNECT_DATA =(SERVICE_NAME = database)));User ID=User/Schema;Password=password;Unicode=True";

& then use this conection string as follow

using (OracleConnection objConn = new OracleConnection(con))
 {
   \\ code
 }
user2148124
  • 940
  • 1
  • 7
  • 20
3

I highly recommend using the "Official Oracle ODP.NET, Managed Driver".

https://www.nuget.org/packages/Oracle.ManagedDataAccess/

Or the one for Entity framework: https://www.nuget.org/packages/Oracle.ManagedDataAccess.EntityFramework/

After installing via Visual Studio it opens a readme that I also recommend you read.

Oracle provide massive amounts of documentation. This is where to start: http://www.oracle.com/technetwork/topics/dotnet/whatsnew/index.html

Kasper Jensen
  • 548
  • 5
  • 12
1

You use System.Data.SqlClient.SqlConnection which is used to connect to a (Microsoft) SQL Server, it cannot be used for an Oracle connection.

You should use System.Data.OracleClient.OracleConnection or Oracle.ManagedDataAccess.Client.OracleConnection

Have a look at this answer to see other possibilities: How to connect to Oracle 11 database from . net

Wernfried Domscheit
  • 54,457
  • 9
  • 76
  • 110
0

You can always use EF, and use create one ADO.Net Entity Data Model

enter image description here

And use the wizard to create and test the connection

enter image description here

Juan Carlos Oropeza
  • 47,252
  • 12
  • 78
  • 118