Click here to Skip to main content
15,892,517 members
Articles / Programming Languages / C#
Alternative
Tip/Trick

Programatically update connectionstring using C#

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
15 Nov 2011CPOL 10.8K   3
var configuration = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("~");var section = (System.Configuration.ConnectionStringsSection)configuration.GetSection("connectionStrings");section.ConnectionStrings["MySqlConnection"].ConnectionString = "Data Source=" + ...
C#
var configuration = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("~");
var section = (System.Configuration.ConnectionStringsSection)configuration.GetSection("connectionStrings");

section.ConnectionStrings["MySqlConnection"].ConnectionString = "Data Source=" + 
        txtserver.Text + ";Initial Catalog=" + txtdatabase.Text + ";User ID=" + txtuid.Text + 
        ";Password=" + txtpwd.Text;
configuration.Save();

License

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


Written By
Technical Lead
India India
Project Lead
MCTS - .NET Framework 4.0, Web Applications

Blog : http://thakkermukund.wordpress.com
Twitter@thakkermukund

Don't code today, what you can't debug tomorrow!
Everything makes sense in someone's mind

Comments and Discussions

 
GeneralDoes anyone have an example of where the Web.Config AND the ... Pin
SteveMets6-Dec-11 10:32
professionalSteveMets6-Dec-11 10:32 
GeneralI suggest you use the ConnectionStringBuilder class instead ... Pin
bbirajdar15-Nov-11 3:16
bbirajdar15-Nov-11 3:16 
I suggest you use the ConnectionStringBuilder class instead of ' section.ConnectionStrings["MySqlConnection"].ConnectionString = "Data Source=" + txtserver.Text + ";Initial Catalog=" + txtdatabase.Text + ";User ID=" + txtuid.Text + ";Password=" + txtpwd.Text;
'
GeneralNot tested yet. But approved by looking at the code. Pin
bbirajdar15-Nov-11 3:15
bbirajdar15-Nov-11 3:15 

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.