Click here to Skip to main content
12,702,791 members (31,265 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


1 bookmarked

Programatically update connectionstring using C#

, 8 Nov 2011 CPOL
Rate this:
Please Sign up or sign in to vote.
You can do the same using the configuration-specific parts of the .NET Framework to save some of the hard work:// Get the configuration file.System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);//Initialize the Connection...

You can do the same using the configuration-specific parts of the .NET Framework to save some of the hard work:

// Get the configuration file.
System.Configuration.Configuration config = 

//Initialize the Connection string builder
SqlConnectionStringBuilder sqlBuilder = new SqlConnectionStringBuilder
   InitialCatalog = "YOUR_DATABASE",
   IntegratedSecurity = true,
   Password = "YOUR_PASSWORD"

// Add the connection string.
config.ConnectionStrings.ConnectionStrings["CONNECTION_NAME"].ConnectionString = 

// Save the configuration file.

It'll achieve the same result provided you have the right permissions, only you don't have to treat the config file as a generic XmlDocument and parse the items to find the correct section.


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


About the Author

jim lahey
Software Developer (Senior)
United Kingdom United Kingdom
First batch file in 1987

Messed around with the Bullfrog C++ Libraries for Syndicate in 1994

Web Developer since 2000

.net Developer since 2001

MCTS: Microsoft® .NET Framework 2.0 - Web-based Client Development

MCTS: Web Applications Development with Microsoft .NET Framework 4

You may also be interested in...


Comments and Discussions

GeneralMy app.config file <code> <configuration> <configsections... Pin
Pritesh Aryan16-Nov-11 0:33
memberPritesh Aryan16-Nov-11 0:33 
GeneralReason for my vote of 2 This gives following error. exePath ... Pin
Mukund Thakker15-Nov-11 1:24
memberMukund Thakker15-Nov-11 1:24 
GeneralSystem.Configuration.Configuration config = System.Configura... Pin
Mukund Thakker15-Nov-11 1:22
memberMukund Thakker15-Nov-11 1:22 
GeneralReason for my vote of 5 excellent alternative..thanks Pin
B Birajdar8-Nov-11 10:14
memberB Birajdar8-Nov-11 10:14 

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.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170118.1 | Last Updated 8 Nov 2011
Article Copyright 2011 by jim lahey
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid