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

前端 未结 4 1418
粉色の甜心
粉色の甜心 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:37

    Use local IIS web server instead of Visual Studio Development server (Project Settings - WEB) did the trick for me!

    Tns-12154 had me pulling my hair out... web site worked fine in VS2008...

    Regards,

    Mike

    0 讨论(0)
  • 2021-01-21 06:53

    The code that I´m using is the above.

    P.S.: I´ve tested many times. Using Visual Studio .Net 2010 (VB.Net 2010).

    Dim conn As New Odbc.OdbcConnection
    Dim cmd As New Odbc.OdbcCommand
    Dim drResult As Odbc.OdbcDataReader
    Dim connString As String
    Dim QuerySQL As String
    
    connString = "Driver={Microsoft ODBC for Oracle};CONNECTSTRING=(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=ORACLEDB01)(PORT=1521))(CONNECT_DATA=(SERVICE_NAME=ORACLE_INSTANCE_NAME)));Uid=john;Pwd=mypassword;"
    QuerySQL = "select first_name, last_name from employees where id = 28"
    
    conn.ConnectionString = connString
    conn.Open()
    cmd.Connection = conn
    cmd.CommandText = QuerySQL
    drResult = cmd.ExecuteReader()
    
    While drResult.Read
        TextBox1.Text = TextBox1.Text & drResult("last_name") & ", " & drResult("first_name") & Environment.Newline
    End While
    drResult.Close()
    
    0 讨论(0)
  • 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.");
                }
            }
        }
    }
    
    0 讨论(0)
  • 2021-01-21 07:01

    You should use the Oracle Data Access Client library, and then the OracleConnection object instead.

    http://www.oracle.com/technology/sample_code/tech/windows/odpnet/howto/connect/index.html

    I know that Oracle is kind of picky with TNS names file. I usually count on the DBA's for this. SQL-Server is much easier to get going...

    0 讨论(0)
提交回复
热议问题