Home > DeveloperSection > Articles > Connection String using App.config

Connection String using App.config


.NET .NET 
Ratings:
0 Comment(s)
 15405  View(s)
Rate this:

Connection string using App.config in .NET

I’m going to demonstrate how to get connection string from App.config in C#.Net.

Example

App.config

<?xmlversion="1.0"encoding="utf-8" ?>

<configuration>

  <connectionStrings> 

    <addname="con1"connectionString="Server=abc\SQLEXPRESs;Database=Employee;Trusted_Connection=True"/>  

  </connectionStrings>

</configuration>

Form1.cs where we are going to use connection string of App.config

//namespace for sql client

using System.Data.SqlClient;

//to use connection string we have to use System.Configuration namespace

using System.Configuration;

 

//fetching connection string from App.config

string strConnection = ConfigurationManager.ConnectionStrings["con1"].ConnectionString;

//creating new sqlconnection

            con = new SqlConnection(strConnection);

//opening connection

            con.Open();


Don't want to miss updates? Please click the below button!

Follow MindStick