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

Published on February 7, 2016 by abundantcode

Filed under ADO.NET

Last modified February 6, 2016

Print this page

rate 1 star rate 2 star rate 3 star rate 4 star rate 5 star
Your rating: none, Average: 0 (0 votes)

This article have been viewed 1727 times

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" ?>
        <add name="connection1" connectionString="Data Source=.;Initial Catalog=rajnikanth;IntegratedSecurity=True" providerName="System.Data.SqlClient" />

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;
If you enjoyed this post, please consider leaving a comment or subscribing to the RSS feed to have future articles delivered to your feed reader.

Leave a Comment


You might be Interested in these Posts

How to Convert XDocument to XmlDocument in C# ?

To convert an XmlDocument to XDocument , you can use the CreateReader method of the XDocument and lo...

Thread.Sleep equivalent in UWP App

If you are looking for an equivanet for Thread.Sleep method in UWP apps , you can use the Task.Delay...

Creating Namespace in WinJS for Windows Store App ?

When developing Windows Store App using WinJS library , there are times when you want to create a na...