How to get Connection String from App.Config in C#

Introduction:

Here I will explain how to read or get connection string from app.config file in c#. By using System.Configuration reference we can read or get connectionstring from app.config file in windows or console application in c#.

We always use Connection String in forms that we design but when you design a big software you should set the connection string for once and use it in many forms.

In this method you use the name of the Connection String instead of the connection string text. 

Every time you want to change the connection string just change the main connection string in the App.Config file.

By this method you don’t need to change all of the forms in your project, just change the Connection String in the App.Config.

Use The Code:

<connectionStrings>

<add name="dbconnection" connectionString="DataSource=KaushikDhameliya;Initial Catalog=Kaushik;Integrated Security=true" providerName="System.Data.SqlClient" />

</connectionStrings>

string admin= ConfigurationManager.ConnectionStrings["admin"].ConnectionString;