Read tnsnames.ora in VB.NET

Tuesday, 2 April 2013

Read tnsnames.ora in VB.NET

Below are the code for read tnsname.ora file(tns name) in Through below functions you can get the information of the tns.

//Declare namespace
Imports System
Imports System.Collections.Generic
Imports System.Text.RegularExpressions
Imports System.IO

Public Class ReadTNS
    //Function for Load TNS Name
    Public Function LoadTNSNames() As List(Of String)
        Dim DBNamesCollection As New List(Of String)()
        Dim regPattern As String = "[\n][\s]*[^\(][a-zA-Z0-9_.]+[\s]*"
        Dim tnsNamesOraFilePath As String = GetPathToTNSNamesFile()

        If Not tnsNamesOraFilePath.Equals("") Then
            ' Verify file exists
            Dim tnsNamesOraFile As New FileInfo(tnsNamesOraFilePath)
            If tnsNamesOraFile.Exists Then
                If tnsNamesOraFile.Length > 0 Then
                    'read tnsnames.ora file                        
                    Dim tnsNamesContents As String = File.ReadAllText(tnsNamesOraFile.FullName)
                    Dim numMatches As Integer = Regex.Matches(tnsNamesContents, regPattern).Count
                    Dim col As MatchCollection = Regex.Matches(tnsNamesContents, regPattern)
                    For Each match As Match In col
                        Dim m As String = match.ToString()
                        m = m.Trim()
                End If
            End If
        End If
        Return DBNamesCollection
    End Function

   //Function for Get path of TNS File

    Private Shared Function GetPathToTNSNamesFile() As String
        Dim systemPath As String = Environment.GetEnvironmentVariable("Path")
        Dim reg As New Regex("[a-zA-Z]:\\[a-zA-Z0-9\\]*(oracle|app)[a-zA-Z0-9_.\\]*(?=bin)")
        Dim col As MatchCollection = reg.Matches(systemPath)

        Dim subpath As String = "network\ADMIN\tnsnames.ora"
        For Each match As Match In col
            Dim path As String = match.ToString() & subpath
            If File.Exists(path) Then
                Return path
            End If
        Return String.Empty
    End Function
End Class


  1. Replies
    1. Big data is a term that describes the large volume of data – both structured and unstructured – that inundates a business on a day-to-day basis. big data projects for students But it’s not the amount of data that’s important.Project Center in Chennai

      Spring Framework has already made serious inroads as an integrated technology stack for building user-facing applications. Corporate TRaining Spring Framework the authors explore the idea of using Java in Big Data platforms.

      Spring Training in Chennai

      The new Angular TRaining will lay the foundation you need to specialise in Single Page Application developer. Angular Training

  2. You are the best. Thanks a lot.

  3. Hi, Great.. Tutorial is just awesome..It is really helpful for a newbie like me.. I am a regular follower of your blog. Really very informative post you shared here. Kindly keep blogging. If anyone wants to become a .Net developer learn from Dot Net Training in Chennai. or learn thru Dot Net Training in Chennai. Nowadays Dot Net has tons of job opportunities on various vertical industry.
    or Javascript Training in Chennai. Nowadays JavaScript has tons of job opportunities on various vertical industry.

  4. Thanks a lot very much for the high quality and results-oriented help. I won’t think twice to endorse your blog post to anybody who wants and needs support about this area.
    java training in bangalore

    java training in bangalore

  5. Only here are good game slots the casino best Come to us and get your winnings.

  6. Oh, that's Great Post Thanks for sharing with us.
    This Big Data Hadoop Training in Pune Platform helped me to learn advanced concepts of all the trending technologies out there in the current IT market. really worth trying them.

  7. Brilliant article. The information I have been searching precisely. It helped me a lot, thanks. Keep coming with more such informative article. Would love to follow them...
    hardware and networking training in chennai

    hardware and networking training in omr

    xamarin training in chennai

    xamarin training in omr

    ios training in chennai

    ios training in omr

    iot training in chennai

    iot training in omr

  8. This was a very informative article, indeed loved to read and clear my doubts. Keep us posted a lot more blogs. Also check out our blog pages too.

    data science training in chennai

    ccna training in chennai

    iot training in chennai

    cyber security training in chennai

    ethical hacking training in chennai