1

I know there is some similar issue.

But, still the question is not answered! I need to modify the connection string not add a new one.

Community
  • 1
  • 1
  • Please be more specific. Can you explain why you want to change the string? – StingyJack Nov 24 '08 at 12:50
  • Well, imagine this case. You have one Business Application that supports multi companies, each company has a different database, so, when the user login to the system, he/she has to select the DB first. That's why I need to change the DB file. –  Nov 24 '08 at 15:11
  • As well, if your application is client base, and you connect to your server application that resides on one server, you need to change the connection string at run time, in order to connect to the server application –  Nov 24 '08 at 15:13

2 Answers2

6

This will help you out!

        /// <summary>
        /// Add a connection string to the connection
        /// strings section and store it in the
        /// configuration file. 
        /// </summary>
        /// <param name="csName">The name of the property.</param>
        /// <param name="connectionString">The connectionstring as specified.</param>
        public static void AddConnectionStrings(string csName, string connectionString)
        {

            // Get the configuration file.
            System.Configuration.Configuration config =
                ConfigurationManager.OpenExeConfiguration(
                ConfigurationUserLevel.None);

            // Add the connection string.
            ConnectionStringsSection csSection = config.ConnectionStrings;
            csSection.ConnectionStrings.Add(
                new ConnectionStringSettings(csName,
                    connectionString, "System.Data.SqlClient"));

            // Save the configuration file.
            config.Save(ConfigurationSaveMode.Full);       
        }

Well for updating it was hard to find any usefull code to update a connectionstring. It wasn't possible to update a connectionstring, so I had the remove the connectionstring, and add a new one. Well isn't that odd?? Microsoft left something undone... Well maybe it isn't usefull to change your app.config, but well, I had to make it anyhow. So dynamic app.config or web.config files is little bit strange, because you can't dynamically update it. No you have to remove it first by the configurationmanager.

        /// <summary>
        /// First remove the old connectionstring and after that
        /// add a connection string to the connectionstrings
        /// section and store it in the configuration file. 
        /// </summary>
        /// <param name="csName">The name of the property.</param>
        /// <param name="connectionString">The connectionstring as specified.</param>
        public static void UpdateConnectionStrings(string csName, string connectionString)
        {
            // Get the configuration file
            System.Configuration.Configuration config =
                ConfigurationManager.OpenExeConfiguration(
                ConfigurationUserLevel.None);

            // Remove the existing connectionstring.
            config.ConnectionStrings.ConnectionStrings.Remove(csName);
            // Add the connectionstring
            ConnectionStringsSection csSection = config.ConnectionStrings;
            csSection.ConnectionStrings.Add(
                new ConnectionStringSettings(csName, 
                connectionString, "System.Data.SqlClient"));

            // Save the configuration file
            config.Save(ConfigurationSaveMode.Full);
        }
MysticSlayer
  • 380
  • 3
  • 7
0

This is an oldish question, and I would just like to know what solution eventually worked out for you. If you havent resolved it yet, this question attempts to answer it.

Community
  • 1
  • 1
Shalan
  • 943
  • 4
  • 20
  • 43