You can change them as follows:
Properties.Settings.Default["MyConnectionString"] = newCnnStr;
For a complete solution that also saves the new value in a file, you need to do something like this:
private static void ModifyConnectionStrings() {
If your dataset is in a different assembly, you can still do this by providing public settings for this assembly. For this:
- Right-click the project in Solution Explorer and select Properties
- Go to the Settings tab.
- Change the Access Modifier drop-down menu to "Public", save and close.
Then you can do this (assuming another project is called "MyProject.DataLayer"):
private static void ModifyConnectionStrings() { // Change the value in the config file first var config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); const string newCnnStr = "server=(local);database=MyDb;user id=user;password=secret"; config.ConnectionStrings.ConnectionStrings["MyProject.Properties.Settings.MyConnectionString"].ConnectionString = newCnnStr; config.ConnectionStrings.ConnectionStrings["MyProject.DataLayer.Properties.Settings.MyConnectionString"].ConnectionString = newCnnStr; config.Save(ConfigurationSaveMode.Modified, true); // Now edit the in-memory values to match Properties.Settings.Default["MyConnectionString"] = newCnnStr; MyProject.DataLayer.Properties.Settings.Default["MyConnectionString"] = newCnnStr; }
source share