VS2005 C# 以编程方式更改 app.config 中包含的连接字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/63546/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 10:21:55  来源:igfitidea点击:

VS2005 C# Programmatically change connection string contained in app.config

提问by Murray Van Wieringen

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?

想以编程方式更改数据库的连接字符串,该数据库在 Windows 应用程序中使用 asp.net 的成员资格提供程序。system.configuration 命名空间允许更改用户设置,但是,我们想调整应用程序设置吗?是否需要编写一个类,利用 XML 来修改该类?是否需要删除当前连接(可以选择要清除的连接)并添加新连接吗?可以调整现有的连接字符串吗?

回答by Joseph Daigle

You can programatically open the configuration with using the System.configuration namespace:

您可以使用 System.configuration 命名空间以编程方式打开配置:

Configuration myConfig = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);

Configuration myConfig = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);

Then you can access the connection strings collection at:

然后您可以在以下位置访问连接字符串集合:

myConfig.ConnectionStrings.ConnectionStrings

myConfig.ConnectionStrings.ConnectionStrings

You can modify the collection however you want, and when done call .Save()on the configuration object.

您可以根据需要修改集合,完成后调用.Save()配置对象。

回答by Dave Van den Eynde

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.

使用 ConnectionStringsSection 类。该文档甚至提供了一个示例,说明如何创建新的 ConnectionString 并让框架将其保存到配置文件中,而无需实现整个 XML shebang。

See hereand browse down for an example.

请参阅此处并向下浏览以获取示例。

回答by dpollock

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

// Create a connection string element and
// save it to the configuration file.

// Create a connection string element.
ConnectionStringSettings csSettings =
        new ConnectionStringSettings("My Connection",
        "LocalSqlServer: data source=127.0.0.1;Integrated Security=SSPI;" +
        "Initial Catalog=aspnetdb", "System.Data.SqlClient");

// Get the connection strings section.
ConnectionStringsSection csSection =
    config.ConnectionStrings;

// Add the new element.
csSection.ConnectionStrings.Add(csSettings);

// Save the configuration file.
config.Save(ConfigurationSaveMode.Modified);

回答by Bradley Mountford

Had to do this exact thing. This is the code that worked for me:

不得不做这件事。这是对我有用的代码:

var config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
var connectionStringsSection = (ConnectionStringsSection)config.GetSection("connectionStrings");
connectionStringsSection.ConnectionStrings["Blah"].ConnectionString = "Data Source=blah;Initial Catalog=blah;UID=blah;password=blah";
config.Save();
ConfigurationManager.RefreshSection("connectionStrings");