Click here to Skip to main content
15,890,506 members
Articles / Programming Languages / C# 4.0
Tip/Trick

Enumerate entries in TNSNames.ora using Oracle data provider class and DataSourceEnumerator

Rate me:
Please Sign up or sign in to vote.
4.45/5 (5 votes)
12 Jul 2011CPOL 30.2K   4   4
There is an easier way to get the Oracle data source than custom-code a TNSNames.ora parser.

Enumerating and writing out the entries in the default TNSNames.ora pointed to in by your system path is simple:



  • Download the Oracle data provider for .NET (ODP.NET) and install (download 32-bit or 64-bit based on your needs)
  • Make a reference to Oracle.DataAccess.

C#
using System;
using System.Data;
using System.Data.Common;
using Oracle.DataAccess.Client;
class DataSourceEnumSample
{
    static void Main()
    {
        string ProviderName = "Oracle.DataAccess.Client";
        DbProviderFactory factory = DbProviderFactories.GetFactory(ProviderName);
        if (factory.CanCreateDataSourceEnumerator)
        {
            DbDataSourceEnumerator dsenum = factory.CreateDataSourceEnumerator();
            DataTable dt = dsenum.GetDataSources();
            foreach (DataRow row in dt.Rows)
            {
                System.Diagnostics.Debug.Print(dt.Columns[0] + " : " + row[0]);
                System.Diagnostics.Debug.Print(dt.Columns[1] + " : " + row[1]);
                System.Diagnostics.Debug.Print(dt.Columns[2] + " : " + row[2]);
                System.Diagnostics.Debug.Print(dt.Columns[3] + " : " + row[3]);
                System.Diagnostics.Debug.Print(dt.Columns[4] + " : " + row[4]);
                System.Diagnostics.Debug.Print("--------------------");
            }            
        }
        else
            Console.Write("Data source enumeration is not supported by provider");
    }
}

Here is part of the output (with names changed to protect the innocent)


InstanceName : Dev.WORLD
ServerName : Dev-instance.mycompany.com
ServiceName : DEV
Protocol : TCP
Port : 1521
--------------------
InstanceName : QA
ServerName : QA-instance.mycompany.com
ServiceName : QA
Protocol : TCP
Port : 1521
--------------------
InstanceName : Prod
ServerName : prod-instance.mycompany.com
ServiceName : Prod
Protocol : TCP
Port : 1521

Please note that I did not code the above. It is in the Oracle documentation. Just search for "Oracle data provider - Developer's Guide".


If you are interested in enumerating SQL Server's, just change the provider string:


C#
string ProviderName = "System.Data.SqlClient";

Have fun coding!

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect Transamerica Reinsurance
United States United States
Oracle and SQL Server Database Administrator. Was as database architect before now. Love databases in general and my life has revolved around them. Other than PL/SQL and T-SQL, I also program in C# mostly for fun than any other reason. Occasionally, I speak and learn at SQL Saturday events (on SQL Server by PASS).

Comments and Discussions

 
QuestionDoes not appear to work with the instant client. Pin
Sing Abend13-Mar-14 8:27
professionalSing Abend13-Mar-14 8:27 
Does not appear to work with the instant client. The factory CanCreateDataSourceEnumerator is false;
Any work around?
AnswerRe: Does not appear to work with the instant client. Pin
Jana Sattainathan8-Jul-14 2:59
Jana Sattainathan8-Jul-14 2:59 
QuestionTnsNames.ora Location Pin
shoelace17-Oct-13 6:56
shoelace17-Oct-13 6:56 
AnswerRe: TnsNames.ora Location Pin
Sing Abend13-Mar-14 9:05
professionalSing Abend13-Mar-14 9:05 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.