VS2005 C# 以编程方式更改 app.config 中包含的连接字符串 [英] VS2005 C# Programmatically change connection string contained in app.config

查看:11
本文介绍了VS2005 C# 以编程方式更改 app.config 中包含的连接字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

希望以编程方式更改数据库的连接字符串,该数据库在 Windows 应用程序中使用 asp.net 的成员资格提供程序.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?

推荐答案

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

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 C# 以编程方式更改 app.config 中包含的连接字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆