How to Get Connection String from App.config file in C# ?

If you want to get the connection string from the App.config file in C# , you can use the ConfigurationManager class to achieve the same.

How to Get Connection String from App.config file in C# ?

Assume that the configuration file contains the following

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <connectionStrings>
        <add name="connection1" connectionString="Data Source=.;Initial Catalog=rajnikanth;IntegratedSecurity=True" providerName="System.Data.SqlClient" />
    </connectionStrings>
</configuration>

Below is the code snippet to get the connection string.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;

namespace ACCode
{
    class Program
    {
        static void Main(string[] args)
        {
            // Gets the connection string from the System.Configuration.ConfigurationManager class.
            var connection = System.Configuration.ConfigurationManager.ConnectionStrings["connection1"].ConnectionString;
            Console.ReadLine();
        }       
    } 
}