Click here to Skip to main content
Click here to Skip to main content

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

By , 5 Jul 2012
 

Introduction

We always use connection string in forms that we design but when you design a big software you should set connection string for once and use it in many forms  in this method you use name of connection string in stead of connection string text. 

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

By this method you don't need to change all of forms in your project and just change the connection string in App.Config.

Using the Code

First of all you should set the connection string in App.Config File.

For example I set the connection string for my database as you see down:

<?xml version="1.0"?>
<configuration>
  <connectionStrings>
    <add name="CharityManagement" 
    connectionString="Data Source=.;Initial Catalog=CharityManagement;Integrated Security=True"/>
  </connectionStrings>
</configuration>

After that you use connection string in your forms of project by 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 Connection String from App.Config by use ConnectionStrings properties.

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

You can use this way in both of C# and ASP.NET Projects.

License

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

About the Author

Alireza Chegini
Software Developer
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.
Follow on   Google+

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralMy vote of 1membercsd21-May-13 22:13 
Not working. pls be sure of ur solutions to problems b4 posting.
GeneralMy vote of 5memberMember 850885518-Feb-13 1:19 
It is complete
NewsThe problem of the location of the app.config file.memberAmitMukherjee13-Feb-13 23:46 
The second problem was the location of the app.config file.
In the Q3 VS2008 SP2, the connection section from that file needs to be moved to the program's main app.config file to be accessed. Pretty simple and obvious after you spend some time debugging but this may save someone the minutes needed for that exercise.
SuggestionAdd Referencememberleonelbeira6-Jan-13 4:45 
Just to Add,sometimes you the code will have error if is your first time working with App.config,
 
you must add System.Configuration Reference to your project
 
Regards.
QuestionIf you face ProblemmemberSarveh kumar19-Sep-12 1:22 
Use this code to get connection string from app.config...
 
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <configSections>
    </configSections>
  <appSettings>
    <add key="con" value="Data Source=KARUNESH-PC;Initial Catalog=school_inventry;User ID=sa;Password=hpes"/>
  </appSettings>
</configuration>
 

To get Conn. string......
 
string s =System.Configuration.ConfigurationSettings.AppSettings.Get("con");

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

Permalink | Advertise | Privacy | Mobile
Web02 | 2.6.130617.1 | Last Updated 5 Jul 2012
Article Copyright 2012 by Alireza Chegini
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid