Click here to Skip to main content
5,788,961 members and growing! (20,205 online)
Email Password   helpLost your password?
Database » Database » Oracle     Intermediate License: The Code Project Open License (CPOL)

TNSNames Reader

By ciro_vladimir

An article explaining how to list oracle data sources from tnsnames.ora file
C#, Oracle, DBA, Dev

Posted: 13 Nov 2008
Updated: 13 Nov 2008
Views: 1,616
Bookmarked: 5 times
Announcements
Loading...



Search    
Advanced Search
Sitemap
4 votes for this Article.
Popularity: 1.00 Rating: 1.67 out of 5
2 votes, 50.0%
1
1 vote, 25.0%
2
1 vote, 25.0%
3
0 votes, 0.0%
4
0 votes, 0.0%
5

Introduction

I needed some way to let the user select a datasource to use in one application. We have multiple databases which are defined in the tnsnames.ora file. This article shows a class that helps you list the datasources defined in that file. I just translated the code from VB and found here with some fixes.

The Class

using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Win32;
using System.Text.RegularExpressions;

namespace TNSNamesReader
{
    public class TNSNamesReader
    {
        public List<string> GetOracleHomes()
        {
            List<string> oracleHomes = new List<string>();
            RegistryKey rgkLM = Registry.LocalMachine;
            RegistryKey rgkAllHome = rgkLM.OpenSubKey(@"SOFTWARE\ORACLE");
            if (rgkAllHome != null)
            {
                foreach (string subkey in rgkAllHome.GetSubKeyNames())
                {
                    if (subkey.StartsWith("KEY_"))
                        oracleHomes.Add(subkey);
                }
            }
            return oracleHomes;
        }

        private string GetOracleHomePath(String OracleHomeRegistryKey)
        {
            RegistryKey rgkLM = Registry.LocalMachine;
            RegistryKey rgkOracleHome = rgkLM.OpenSubKey(@"SOFTWARE\ORACLE\" +
                OracleHomeRegistryKey);

                if (!rgkOracleHome.Equals(""))
                    return rgkOracleHome.GetValue("ORACLE_HOME").ToString();
            return "";
        }

        private string GetTNSNAMESORAFilePath(String OracleHomeRegistryKey)
        {
            string oracleHomePath = this.GetOracleHomePath(OracleHomeRegistryKey);
            string tnsNamesOraFilePath = "";
            if (!oracleHomePath.Equals(""))
            {
                tnsNamesOraFilePath = oracleHomePath + @"\NETWORK\ADMIN\TNSNAMES.ORA";
                if (!(System.IO.File.Exists(tnsNamesOraFilePath)))
                {
                    tnsNamesOraFilePath = oracleHomePath + @"\NET80\ADMIN\TNSNAMES.ORA";
                }
            }
            return tnsNamesOraFilePath;
        }

        public List<string> LoadTNSNames(string OracleHomeRegistryKey)
        {
            List<string> DBNamesCollection = new List<string>();
            string RegExPattern = @"[\n][\s]*[^\(][a-zA-Z0-9_.]+[\s]*=[\s]*\(";
            string strTNSNAMESORAFilePath = GetTNSNAMESORAFilePath(OracleHomeRegistryKey);

            if (!strTNSNAMESORAFilePath.Equals(""))
            {
                //check out that file does physically exists
                System.IO.FileInfo fiTNS = new System.IO.FileInfo(strTNSNAMESORAFilePath);
                if (fiTNS.Exists)
                {
                    if (fiTNS.Length > 0)
                    {
                        //read tnsnames.ora file
                        int iCount;
                        for (iCount = 0; iCount < Regex.Matches(
                            System.IO.File.ReadAllText(fiTNS.FullName),
                            RegExPattern).Count; iCount++)
                        {
                            DBNamesCollection.Add(Regex.Matches(
                                System.IO.File.ReadAllText(fiTNS.FullName),
                                RegExPattern)[iCount].Value.Trim().Substring(0,
                                Regex.Matches(System.IO.File.ReadAllText(fiTNS.FullName),
                                RegExPattern)[iCount].Value.Trim().IndexOf(" ")));
                        }
                    }
                }
            }
            return DBNamesCollection;
        }
    }
}

Using the Code

  1. Create an instance of the class in your form.
    private TNSNamesReader tnsNamesReader = new TNSNamesReader();
  2. Populate a list control (combo, listbox, etc.) to let your user select the Oracle Home where the tnsnames.ora file will be read.
    cmbOracleHome.DataSource = tnsNamesReader.GetOracleHomes();
  3. Populate another list control with the datasources found.
    this.cmbDataSource.DataSource = tnsNamesReader.LoadTNSNames(
        (string)this.cmbOracleHome.SelectedValue);

Points of Interest

In the article I pointed out, it's supposed to be a registry key "SOFTWARE\ORACLE\ALL_HOMES" and a subkey called "LAST_HOME." None of them existed on my registry and caused an exception. I went to the windows registry and saw that every oracle home registry key begins with the word "KEY_" so I took every key beggining with this and listed them as Oracle Homes. Once an oracle home is selected we can look for the tnsnames.ora file path and load its data sources. This class doesn't find the Current or Active Oracle Home, however, you must select one.

License

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

About the Author

ciro_vladimir



Occupation: Web Developer
Location: Mexico Mexico

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 2 of 2 (Total in Forum: 2) (Refresh)FirstPrevNext
GeneralOracle version specific...memberWTF_BFS13:39 18 Dec '08  
GeneralRe: Oracle version specific...memberciro_vladimir4:19 19 Dec '08  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 13 Nov 2008
Editor: Sean Ewington
Copyright 2008 by ciro_vladimir
Everything else Copyright © CodeProject, 1999-2009
Web13 | Advertise on the Code Project