Click here to Skip to main content
13,352,161 members (72,513 online)
Click here to Skip to main content
Add your own
alternative version


33 bookmarked
Posted 5 Jul 2012

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

, 15 Oct 2013
Rate this:
Please Sign up or sign in to vote.
This code helps you to grab ConnectionString from App.config.


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.

Using the Code

First of all you should set the connection string in the App.Config file. For example I set the connection string for my database as you see here:

<?xml version="1.0"?>
    <add name="CharityManagement" 

    connectionString="Data Source=.;Initial Catalog=CharityManagement;Integrated Security=True"/>

After that you use the connection string in your forms using this code:

In your forms you set references that you want to use:

using System;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Windows.Forms;

Then you can get the Connection String from the App.Config by using the ConnectionStrings property.

var connectionString=ConfigurationManager.ConnectionStrings["CharityManagement"].ConnectionString;

You can use this method in both Windows Forms and ASP.NET projects.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Alireza C
Software Developer (Senior)
Iran (Islamic Republic of) Iran (Islamic Republic of)
I like programming because when you design a software you create a thing that improve your life.

You may also be interested in...

Comments and Discussions

QuestionNice, quick and clean Pin
James McCullough2-Apr-16 17:21
professionalJames McCullough2-Apr-16 17:21 
Suggestionbetter option Pin
Arkadeep De15-Oct-14 21:26
professionalArkadeep De15-Oct-14 21:26 
Rani Chehadeh2-Oct-14 8:25
memberRani Chehadeh2-Oct-14 8:25 
tara_sh50025-Aug-15 16:02
membertara_sh50025-Aug-15 16:02 
GeneralMy vote of 5 Pin
srilekhamenon15-Mar-14 1:17
membersrilekhamenon15-Mar-14 1:17 
SuggestionUse repository pattern instead Pin
Uldis Rāts17-Oct-13 1:04
memberUldis Rāts17-Oct-13 1:04 
GeneralMy vote of 5 Pin
JasonMacD15-Oct-13 12:03
memberJasonMacD15-Oct-13 12:03 
GeneralRe: My vote of 5 Pin
Sacha Barber15-Oct-13 22:54
mvpSacha Barber15-Oct-13 22:54 
GeneralRe: My vote of 5 Pin
JasonMacD16-Oct-13 3:32
memberJasonMacD16-Oct-13 3:32 
GeneralMy vote of 5 Pin
ho.kurd27-Aug-13 13:52
memberho.kurd27-Aug-13 13:52 
GeneralMy vote of 1 Pin
csd21-May-13 23:13
membercsd21-May-13 23:13 
GeneralRe: My vote of 1 Pin
JasonMacD15-Oct-13 12:02
memberJasonMacD15-Oct-13 12:02 
GeneralRe: My vote of 1 Pin
tlobig14-Apr-15 22:11
membertlobig14-Apr-15 22:11 
GeneralMy vote of 5 Pin
Member 850885518-Feb-13 2:19
memberMember 850885518-Feb-13 2:19 
NewsThe problem of the location of the app.config file. Pin
AmitMukherjee14-Feb-13 0:46
memberAmitMukherjee14-Feb-13 0:46 
SuggestionAdd Reference Pin
leonelbeira6-Jan-13 5:45
memberleonelbeira6-Jan-13 5:45 
QuestionIf you face Problem Pin
Sarveh kumar19-Sep-12 2:22
memberSarveh kumar19-Sep-12 2:22 
AnswerRe: If you face Problem Pin
Venkat Raghvan10-Oct-13 22:33
memberVenkat Raghvan10-Oct-13 22:33 
AnswerRe: If you face Problem Pin
Alireza Chegini15-Oct-13 11:35
memberAlireza Chegini15-Oct-13 11:35 
BugRe: If you face Problem Pin
Member 85834414-Oct-17 21:49
memberMember 85834414-Oct-17 21:49 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.180111.1 | Last Updated 15 Oct 2013
Article Copyright 2012 by Alireza C
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid