无法在应用中添加/更新连接字符串。配置[英] unable to add/update connection string in app.config

本文是小编为大家收集整理的关于无法在应用中添加/更新连接字符串。配置的处理方法,想解了无法在应用中添加/更新连接字符串。配置的问题怎么解决?无法在应用中添加/更新连接字符串。配置问题的解决办法?那么可以参考本文帮助大家快速定位并解决问题。

问题描述

你好,
我正在使用下面的代码在我的 app.config 文件中添加一个新的连接字符串,但它没有更新,也没有抛出任何错误.
当我执行此代码时,消息框仅显示前两个连接字符串,并且 app.config 文件也未更新

尝试{System.Configuration.Configuration 配置 = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);//ConnectionStringSettingsCollection 连接 = ConfigurationManager.ConnectionStrings;//ConfigurationPermission cp = new ConfigurationPermission(System.Security.Permissions.PermissionState.Unrestricted);ConnectionStringSettings css = new ConnectionStringSettings("cString3", "abcdefxyz");config.ConnectionStrings.ConnectionStrings.Add(css);config.Save(ConfigurationSaveMode.Minimal);ConfigurationManager.RefreshSection("connectionStrings");foreach (ConnectionStringSettings 连接 in 连接){string str = connection.Name;string val = connection.ConnectionString;MessageBox.Show(str + " " + val);//textBox1.Text = str;}}catch(例外){MessageBox.Show(ex.Message);}


我的 xml 文件是

xml版本="1.0" 编码="utf-8" ?><配置><appSettings><添加 key="Setting1" ="1" /><添加 key="Setting2" ="2" /></appSettings><connectionStrings><添加 name="cString1" connectionString="第一个连接字符串正确"/><添加 name="cString2" connectionString="第二个连接string"/></connectionStrings></configuration>
解决方案
看看使用 .NET 2.0 读/写 App.Config 文件[^]

最好的问候
Espen Harlinn

查看我的文章.
在 C# 运行时在 App.Config 文件中配置连接字符串[^]

试试{System.Configuration.Configuration 配置 = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);//ConnectionStringSettingsCollection 连接 = ConfigurationManager.ConnectionStrings;//ConfigurationPermission cp = new ConfigurationPermission(System.Security.Permissions.PermissionState.Unrestricted);ConnectionStringSettings css = new ConnectionStringSettings("cString3", "abcdefxyz");config.ConnectionStrings.ConnectionStrings.Add(css);config.Save();ConfigurationManager.RefreshSection("connectionStrings");//这可能是不必要的.foreach (ConnectionStringSettings 连接 in 连接){string str = connection.Name;string val = connection.ConnectionString;MessageBox.Show(str + " " + val);//textBox1.Text = str;}}catch(例外){MessageBox.Show(ex.Message);}


所以,我已经在我的一个项目(Enterprise Library 4.1)上实现了这个:
private static void CopyCurrentDataConfiguration(IDataConfiguration dataConfig){//从配置文件中加载配置var configSource = ConfigurationSourceFactory.Create();//获取默认数据源名称var dataConfigurationSection = (DatabaseSettings)configSource.GetSection(数据库设置.SectionName);var defaultDatabaseName = dataConfigurationSection.DefaultDatabase;CopyConnectionStrings(dataConfig, defaultDatabaseName);}private static void CopyConnectionStrings(IDataConfiguration dataConfig,string defaultDatabaseName){// 将我们的应用程序配置文件中的配置添加到构建器中foreach(ConnectionStringSettings 设置 ConfigurationManager.ConnectionStrings){试试{var 已配置Database = dataConfig.ForDatabaseNamed(settings.Name).那是.AnotherDatabaseType(settings.ProviderName);if (settings.Name.Equals(defaultDatabaseName)){配置数据库.AsDefault();}}捕获 { }}}public static void AddNewConnectionString(string 数据库名称,string connString){var builder = new ConfigurationSourceBuilder();var dataConfig = builder.ConfigureData();CopyCurrentDataConfiguration(dataConfig);//将我们的数据库连接添加到构建器数据配置.ForDatabaseNamed(数据库名).那是.AnotherDatabaseType("Oracle.DataAccess.Client").WithConnectionString(connString).AsDefault();string conn = "your_connection_string";//创建一个新的配置源,用builder更新它并使其成为当前配置var newConfigSource = new DictionaryConfigurationSource();builder.UpdateConfigurationWithReplace(newConfigSource);EnterpriseLibraryContainer.Current = EnterpriseLibraryContainer.CreateDefaultContainer(newConfigSource);}


希望对您有所帮助.

本文地址:https://www.itbaoku.cn/post/1356059.html