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

前端 未结 4 1421
粉色の甜心
粉色の甜心 2021-01-21 06:34

I am using:

  • Visual Studio 2010
  • .Net Framework Data Provider for Oracle
  • Oracle Developer Tools for Visual Studio (from Oracle\'s website)
4条回答
  •  北荒
    北荒 (楼主)
    2021-01-21 06:58

    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.");
                }
            }
        }
    }
    

提交回复
热议问题