.net – VS2010 + Oracle driver: ORA-12154: TSN:could not resolve the connect identifier specified

drivernetoraclevisual studio

I am using:

  • Visual Studio 2010
  • .Net Framework Data Provider for Oracle
  • Oracle Developer Tools for Visual Studio (from Oracle's website)

I tried installing 'Oracle Developer Tools for Visual Studio' and created tnsnames.ora and sqlnet.ora files in my C:\app\ [my username]\product\11.2.0\client_1\Network\Admin directory.

They look like this:

# tnsnames.ora

ORATEST =
  (DESCRIPTION =
    (ADDRESS_LIST =
      (ADDRESS = (PROTOCOL = TCP)(HOST = dbs-oratest)(PORT = 1521))
    )
    (CONNECT_DATA =
      (SID = [ORATEST])
    )
  )

and

# sqlnet.ora

SQLNET.AUTHENTICATION_SERVICES= (ALL)
NAMES.DIRECTORY_PATH= (TNSNAMES, EZCONNECT)

When I try using the .Net Framework Data Provider for Oracle driver to set up a new connection (or any other driver for that matter: OLE, ODBC, etc) it gives the error:

ORA-12154: TSN:could not resolve the connect identifier specified

Using the OLE driver on a machine without the Oracle client installed DOES work though:

OleDbConnection conn = new OleDbConnection(
    "Provider=MSDAORA;Data Source=ORATEST;" + 
    "Persist Security Info=True;Password=readonly;User ID=readonlyuser");

What am I doing wrong? Are there any simple instructions online about how to install a basic Oracle driver?

Thank you in advance!

Best Answer

The best solution I found was to use the Oracle Data Access Client library, and include the entire TNS names entry in the connection string. This allows the project to be easily published to a web server, ClickOnce, etc.

Here are the steps necessary to set up the Oracle driver working in your project:

1) Get DLLs from 'Oracle Data Provider for .NET' package

Download installer file from this location: http://www.oracle.com/technetwork/topics/dotnet/index-085163.html

I went ahead and installed the full 200 MB ODAC with Oracle Developer Tools for Visual Studio, but you only really need four DLLs from this download. (You may be able to extract them directly from the installer package, instead of going through the entire install process, or perhaps one of the smaller download includes all of them.)

2) Reference DLLs in your project

Search the installation directory of the Oracle Data Access Client and drag the following four DLLs into the root of your project:

  • Oracle.DataAccess.dll
  • oci.dll
  • oraciicus11.dll
  • OraOps11w.dll

Set the Copy to Output Directory property all of the files except Oracle.DataAccess.dll to Copy always.

Under Project --> Add Reference..., click on the Browse tab and select the Oracle.DataAccess.dll file.

3) Use the driver with full connection string (optional)

So as not to have to worry about TNS names files being set up on the machines the application was deployed to, I put the entire definition in the file as shown by connectionstrings.com. It makes the connection string a little bulky, but removed a lot of the TNS Names file headaches I was experiencing before:

Data Source=(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=servername)(PORT=‌​1521)))(CONNECT_DATA=(SERVER=DEDICATED)(SERVICE_NAME=servicename)));User Id=username;Password=********;

Here's the full class I used to test the driver:

using System;
using System.Data;
using Oracle.DataAccess.Client;

static class Program
{
    [STAThread]
    static void Main()
    {
        TestOracle();
    }

    private static void TestOracle()
    {
        string connString = 
            "Data Source=(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)" + 
            "(HOST=servername)(PORT=‌​1521)))" +
            "(CONNECT_DATA=(SERVER=DEDICATED)(SERVICE_NAME=servicename)));"+ 
            "User Id=username;Password=********;";
        using (OracleConnection conn = new OracleConnection(connString))
        {
            string sqlSelect = "SELECT * FROM TEST_TABLE";
            using (OracleDataAdapter da = new OracleDataAdapter(sqlSelect, conn))
            {
                var table = new DataTable();
                da.Fill(table);

                if (table.Rows.Count > 1) 
                    Console.WriteLine("Successfully read oracle.");
            }
        }
    }
}
Related Topic