Click here to Skip to main content
13,002,399 members (77,348 online)
Click here to Skip to main content
Add your own
alternative version


17 bookmarked
Posted 6 Feb 2010

How to change ConnectionString easily

, 6 Feb 2010
Rate this:
Please Sign up or sign in to vote.
Changing ConnectionString like an attribute in C#


Everyone who starts a real project in the field of DataBase faces to the ConnectionString changing trouble. For small projects that it is not needed to alter the database location, changing the ConnectionString is not a problem, but in bigger projects specially in the time of application packaging, ConnectionString is a headache.

I have searched for a simple way for changing the ConnectionString in RUN TIME. There are many tips and tricks in Internet, but every way has a side effect.

In this article I will show you how you can change the ConnectionString in Run Time like an ordinary attribute.

Using the code

In .Net programming usually we do not alter the Settings class. This class holds the read only attributes of connection strings. By a magic we can change the connection string in Settings class.

Firstly, we create a User Scoped string Attribute in Settings class: 

public string ConnectionString
    get { return (string)this["ConnectionString"]; }
    set { this["ConnectionString"] = value; }
This attribute holds the connection string in run time. There are some events that can be utilized for changing the main connection string, PropertyChanged and SettingsLoaded. PropertyChanged fires every time the value of any attribute in the class is changed, and SettingsLoaded fires when the loading process of the program settings is finished. We make two event handlers in the constructor of Settings class. Please note that there is no constructor and the constructor should be coded too.
public Settings()
    this.PropertyChanged += 
      new System.ComponentModel.PropertyChangedEventHandler(this.Settings_PropertyChanged);
    this.SettingsLoaded += 
      new System.Configuration.SettingsLoadedEventHandler(this.Settings_SettingsLoaded);
Also, we create two handlers:
private void Settings_PropertyChanged(System.Object sender,
         System.ComponentModel.PropertyChangedEventArgs e)
    if (e.PropertyName == "ConnectionString")
        this["Original_ConnectionString"] = this.ConnectionString;

private void Settings_SettingsLoaded(System.Object sender,
        System.Configuration.SettingsLoadedEventArgs e)
    // Advanced codes for post loading process...
The Original_ConnectionString is the main connection string of your program that you want to change it. Now every where in your code you can assign a new connection string to ConnectionString and it will be your new connection string. Also, you can define a function to do the job. A good place for defining the function is in Program class. You can use a function like this:
public static void ChangeConnectionString(string connectionstring)
    global::[project_namespace].Properties.Settings.Default.ConnectionString = 
By calling the function and passing the connection string to it, you can change the connection string easily. That's it!!!

Points of Interest

You can do more and you can have more flexible code if you use SettingsLoaded. I leave you with them alone!


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


About the Author

Ali Marzban
Software Developer (Senior) AMD
Canada Canada
No Biography provided

You may also be interested in...

Comments and Discussions

QuestionVery good, thank you Pin
Member 1113048629-Nov-16 1:31
memberMember 1113048629-Nov-16 1:31 
QuestionUseful Pin
Olcay Akgün11-Jun-16 19:08
memberOlcay Akgün11-Jun-16 19:08 
QuestionHow use it ? Pin
MVictorL20-Nov-12 1:51
memberMVictorL20-Nov-12 1:51 
QuestionThank You Pin
Member 39321726-Jun-12 23:47
memberMember 39321726-Jun-12 23:47 
Generalhelp full - have 5 Pin
Pranay Rana30-Jan-11 18:52
memberPranay Rana30-Jan-11 18:52 
Generalmy score of 4 Pin
manzo867-Feb-10 21:35
membermanzo867-Feb-10 21:35 
GeneralMy vote of 1 Pin
Antoun Gorgy6-Feb-10 23:24
memberAntoun Gorgy6-Feb-10 23:24 
GeneralRe: My vote of 1 Pin
Ali Marzban7-Feb-10 0:55
memberAli Marzban7-Feb-10 0:55 
GeneralMy vote of 1 Pin
babakzawari6-Feb-10 22:49
memberbabakzawari6-Feb-10 22:49 
GeneralMy vote of 1 Pin
Hamed_ji6-Feb-10 19:27
memberHamed_ji6-Feb-10 19:27 
GeneralRe: My vote of 1 Pin
Roman_K6-Feb-10 19:47
memberRoman_K6-Feb-10 19:47 

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
Web02 | 2.8.170626.1 | Last Updated 6 Feb 2010
Article Copyright 2010 by Ali Marzban
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid