1

I try to connect to an OracleDB from an c# project.

SqlConnection thisConnection = new SqlConnection(
"Server=127.0.0.1:3306;Database=db1;User id=abc;Password=psw;");

While running the code an exeption is thrown:

A first chance exception of type 'System.Data.SqlClient.SqlException' occurred in System.Data.dll
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)

I tried to connect with an local mysql db either and the same exeption is thrown.
I can connect to the server with the Oracle SQL developer tool.

Where is the Problem ?

Soner Gönül
  • 97,193
  • 102
  • 206
  • 364

4 Answers4

2

You are using a SqlConnection (SQL Server) to connect to an Oracle database. See this answer on how to connect to an Oracle database:

https://stackoverflow.com/a/12568350/2382032

As Flindeberg points out, here is the NuGet link:

https://www.nuget.org/packages/odp.net.managed/

Community
  • 1
  • 1
Eric Scherrer
  • 3,328
  • 1
  • 19
  • 34
  • 1
    Eh, no, that is the unmanaged driver, don't use that one. Since about a year back they have a managed driver (ie .NET). – flindeberg Jul 09 '14 at 14:29
  • The answer I link to has a link to the same place as your answer, http://www.oracle.com/technetwork/topics/dotnet/index-085163.html. Thanks though, I will update my answer with the nuget link. I wish they had NuGet back in the day when I had to connect to Oracel! – Eric Scherrer Jul 09 '14 at 14:39
  • Now I can connect to the server but the login fails. Do I have to surround the pasword with special chars or does it work like this: "User ID=username;Password=asd;....."? – user3820836 Jul 10 '14 at 09:02
  • Every example I see here does not surround it with quotes: http://www.connectionstrings.com/oracle/ – Eric Scherrer Jul 10 '14 at 15:08
1

You are using SqlConnection class. This class is used to connect to Microsoft's SQL Server database.

Since the database you're making use of is Oracle's database, you'll need a 'Oracle' specific connection class.

You can make use of OracleDBConnection class though this is not advisable as it has been deprecated.

Instead, you can make use of ODP.NET which is provided & supported by Oracle.

Hope this helps!!!

Satwik Nadkarny
  • 5,086
  • 2
  • 23
  • 41
0

You are using the connector for MS SQL server, don't do that. The only driver you should use for the Oracle Managed ODP.NET driver which you can get at NuGet. Documentation is available here.

Make sure to use the managed driver, and not the unmanaged one.

Example of using it:

var connection = new OracleConnection(str);
using (var command = new OracleCommand(commandStr, connection))
{
   // do stuff with command
}
flindeberg
  • 4,887
  • 1
  • 24
  • 37
0

Try using ODP.Net Driver from NuGet.

Follow the instruction here:
Connect to oracle from c#

Community
  • 1
  • 1
Kent Aguilar
  • 5,048
  • 1
  • 33
  • 20