mark
mark

Reputation: 21

Adding ConnectionString at runtime App.Config and display without reloading application?

We have an internal tool and we need to give the ability to add a connection string programmatically and then reload this connection string without reloading the application at all.

I am kind of confused and wasted 2 days on this and about to give up I have done the following

       var config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
        int initialCount = ConfigurationManager.ConnectionStrings.Count;
        string connStringName = "TEST";
        string serverName="Servedr";
        string databaseName = "MyDb";
        string userId="MyUseId";
        string password="MyPassword";
        var connectionStringBuilder = new SqlConnectionStringBuilder
                                          {
                                              DataSource = serverName,
                                              InitialCatalog = databaseName,
                                              UserID = userId,
                                              Password = password
                                          };


        var csSetting = new ConnectionStringSettings(connStringName, connectionStringBuilder.ConnectionString, "System.Data.SqlClient");
        var csSection = config.ConnectionStrings;
        csSection.ConnectionStrings.Add(csSetting);
        config.Save(ConfigurationSaveMode.Modified, true);
        ConfigurationManager.RefreshSection("ConnectionStrings");

        int finalCount = ConfigurationManager.ConnectionStrings.Count;

This should work no? RefreshSection etc... Any suggestions? workarounds without restarting?

Thanks

Upvotes: 2

Views: 2635

Answers (3)

Rawden Hoff
Rawden Hoff

Reputation: 121

http://www.csharpbydesign.com/2008/01/overriding-dataset-settings-co.html

This worked for me...

After doing some digging I found the following code to place in Settings.cs [Go to settings, then click "View Code"].

Then override the Item property.

Upvotes: 0

Pag Sun
Pag Sun

Reputation: 875

How about using the reflect method like following code snippets:

        var csSetting = new ConnectionStringSettings(connStringName, connectionStringBuilder.ConnectionString, "System.Data.SqlClient");

        var readonlyField = typeof(ConfigurationElementCollection).GetField("bReadOnly", BindingFlags.NonPublic | BindingFlags.Instance);
        readonlyField.SetValue(ConfigurationManager.ConnectionStrings, false);

        var baseAddMethod = typeof(ConfigurationElementCollection).GetMethod("BaseAdd",
            BindingFlags.Instance | BindingFlags.NonPublic, null, new Type[] { typeof(ConfigurationElement) }, null);
        baseAddMethod.Invoke(ConfigurationManager.ConnectionStrings, new object[] { csSetting });

        readonlyField.SetValue(ConfigurationManager.ConnectionStrings, true);

        int finalCount = ConfigurationManager.ConnectionStrings.Count;

Upvotes: 5

Martin Beeby
Martin Beeby

Reputation: 4599

Why not Load the connection string into a static variable. You could Initialize the variable with the connection string in the app.config. You could then easily change the varible at runtime.

You could have your application save the static variable to the app.config when it is closing down.

Upvotes: 0

Related Questions