<p>Would like to programmically change the connecton string for a database which utilizes the membership provider of asp.net within a windows application. The system.configuration namespace allows changes to the user settings, however, we would like to adjust a application setting? Does one need to write a class with utilizes XML to modify the class? Does one need to delete the current connections (can one select a connection to clear) and add a new one? Can one adjust the existing connection string?</p>

[{'answer_id': 63579, 'author': 'Joseph Daigle', 'author_id': 507, 'author_profile': 'https://Stackoverflow.com/users/507', 'pm_score': 3, 'selected': False, 'text': '<p>You can programatically open the configuration with using the System.configuration namespace: </p>\n\n<p><code>Configuration myConfig = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);</code></p>\n\n<p>Then you can access the connection strings collection at:</p>\n\n<p><code>myConfig.ConnectionStrings.ConnectionStrings</code></p>\n\n<p>You can modify the collection however you want, and when done call <code>.Save()</code> on the configuration object.</p>\n'}, {'answer_id': 63639, 'author': 'Dave Van den Eynde', 'author_id': 455874, 'author_profile': 'https://Stackoverflow.com/users/455874', 'pm_score': 0, 'selected': False, 'text': '<p>Use the ConnectionStringsSection class. The documentation even provides an example on how to create a new ConnectionString and have the framework save it to the config file without having to implement the whole XML shebang.</p>\n\n<p>See <a href="http://msdn.microsoft.com/en-us/library/system.configuration.connectionstringssection(VS.80).aspx" rel="nofollow noreferrer">here</a> and browse down for an example.</p>\n'}, {'answer_id': 63674, 'author': 'dpollock', 'author_id': 7884, 'author_profile': 'https://Stackoverflow.com/users/7884', 'pm_score': 3, 'selected': False, 'text': '<pre><code>// Get the application configuration file.\nSystem.Configuration.Configuration config =\n        ConfigurationManager.OpenExeConfiguration(\n        ConfigurationUserLevel.None);\n\n// Create a connection string element and\n// save it to the configuration file.\n\n// Create a connection string element.\nConnectionStringSettings csSettings =\n        new ConnectionStringSettings("My Connection",\n        "LocalSqlServer: data source=127.0.0.1;Integrated Security=SSPI;" +\n        "Initial Catalog=aspnetdb", "System.Data.SqlClient");\n\n// Get the connection strings section.\nConnectionStringsSection csSection =\n    config.ConnectionStrings;\n\n// Add the new element.\ncsSection.ConnectionStrings.Add(csSettings);\n\n// Save the configuration file.\nconfig.Save(ConfigurationSaveMode.Modified);\n</code></pre>\n'}, {'answer_id': 8796298, 'author': 'Bradley Mountford', 'author_id': 302103, 'author_profile': 'https://Stackoverflow.com/users/302103', 'pm_score': 3, 'selected': False, 'text': '<p>Had to do this exact thing.  This is the code that worked for me:</p>\n\n<pre><code>var config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);\nvar connectionStringsSection = (ConnectionStringsSection)config.GetSection("connectionStrings");\nconnectionStringsSection.ConnectionStrings["Blah"].ConnectionString = "Data Source=blah;Initial Catalog=blah;UID=blah;password=blah";\nconfig.Save();\nConfigurationManager.RefreshSection("connectionStrings");\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/63546', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/7950/']