SqlConnection.Open throws error when trying to connect to Oracle server

时间:2015-07-28 15:58:36

标签: c# .net oracle oracle11g

I am trying to connect to Oracle database on my local machine. I have Visual Studio 2013 and Oracle Express 11g.

Here's my c# code:

    String str =
        "Data Source=localhost;" +
        "Initial Catalog=XE;" +
        "User Id=system;" +
        "Password=12345;";
        SqlConnection conn = new SqlConnection(str);
        conn.Open();

However it throws an exception:

An unhandled exception of type 'System.Data.SqlClient.SqlException' occurred in System.Data.dll

Additional information: 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

Does anybody know what's going wrong?

2 个答案:

答案 0 :(得分:1)

Check your provider, as I commented above....

Taken from Oracle Web Site...

string oradb = "Data Source=(DESCRIPTION="
             + "(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=ORASRVR)(PORT=1521)))"
             + "(CONNECT_DATA=(SERVER=DEDICATED)(SERVICE_NAME=ORCL)));"
             + "User Id=***;Password=***;";

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

You may want to look at ConnectionStrings.com to help with which suits you best.

答案 1 :(得分:0)

The problem is that you're trying to use the classes in the System.Data.SqlClient namespace with Oracle. SqlClient is only for Sql Server. You need to use either an Oracle .Net provider or the classes in the System.Data.OleDb namespace.