#编程VS2005碳变化的连接字符串包含在的app.config [英] VS2005 C# Programmatically change connection string contained in app.config
问题描述
想programmically改变connecton字符串,它利用asp.net内的Windows应用程序的成员资格提供程序的数据库。该System.Configuration命名空间可以改变用户的设置,但是,我们要调整一个应用程序的设置?做一件需要与利用XML来修改类写一个类?做一件需要删除当前连接(一个可以选择的连接清除),并添加一个新的?可以调整现有的连接字符串?
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?
推荐答案
不得不这样做确切的事情。这是为我工作的code:
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");
这篇关于#编程VS2005碳变化的连接字符串包含在的app.config的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!