HOW I'M GOING TO INCLUDE CONNECTION FILE IN VB.NET?

Manoj Bhatt

Total Post:154

Points:1086
Posted by  Manoj Bhatt
VB.Net 
 1174  View(s)
Ratings:
Rate this:
Hi Expert,

I'm using vb.net and oracle. this is my code connection with db

I want to establish connection string in VB.NET with Oracle database as following   

Dim connectionString As String = "Data Source = abc; User ID = abc; Password = abc;"
Dim sqlConnection As OracleClient.OracleConnection = New OracleClient.OracleConnection(connectionString)

It's work fine, but when I put connection string into apps.config, that's not working. My line of code as following

<add key="abc_connection" value="server=xyz;User ID=abc;Password=abc;database=abc;Connection Timeout=0"/>

what line of code I should use for replacing my code connection above ? so that i can call the apps.config file instead of using the code connection repeatedly.

Thanks in advance. 
  1. AVADHESH PATEL

    Post:604

    Points:4228
    Re: how i'm going to include connection file in vb.net?

    Hi Manoj,


    You can create a class with settings and add a property

    web config
    <connectionStrings>
    <add name="connectionstring" connectionString="server=xyz;UserID=abc;Password=abc;database=abc;Connection Timeout=0" />

    cs code

        public static string connStr
        { get { return ConfigurationManager.ConnectionStrings["abc_connection"].ConnectionString; } }
    if you want to use key in appconfig

     public static string abc_connection
        { get {return  ConfigurationManager.AppSettings["abc_connection"].ToString(); } }
    in vb

      Public Shared Function connStr() As String
        Return ConfigurationManager.ConnectionStrings("abc_connection").ConnectionString ''
    End Function

    Public Shared Function abc_connection() As String
        Return ConfigurationManager.AppSettings("abc_connection") ''
    End Function

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!