我可以添加到的ConnectionStrings在运行时ConnectionStringCollection? [英] Can I Add ConnectionStrings to the ConnectionStringCollection at Runtime?

查看:131
本文介绍了我可以添加到的ConnectionStrings在运行时ConnectionStringCollection?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法在那里我可以一个连接字符串添加到在运行时一个Asp.Net应用程序通过ConfigurationManager中返回的ConnectionStringCollection?

我曾尝试以下,但我告知的配置文件是只读的。

  ConfigurationManager.ConnectionStrings.Add(新ConnectionStringSettings(PARAMS));

有另一种方式在运行时做到这一点?我知道在设计时,我可以添加一个连接字符串到web.config;不过,我正在寻找的东西在运行时添加到收藏。

感谢

编辑:
一,为什么我试图做到这一点的原因是由于安全要求,即prevents我从放置的ConnectionStrings在web.config中(即使是加密的)。我想用我的项目成员一样的元素和配置文件;不过,我寻找到一个替代做这样的w / o编写自定义提供。自定义提供商是不是所有的坏,但如果我能找到一个简单的解决方案,我完全赞成。


解决方案

  VAR CFG = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration(@/);
cfg.ConnectionStrings.ConnectionStrings.Add(新ConnectionStringSettings(PARAMS));cfg.Save();

被告知这将导致你的网站来回收,因为它修改配置文件。请查看 http://msdn.microsoft.com/en -us /库/ 4c2kcht0(VS.80)的.aspx

Is there a way where I can add a connection string to the ConnectionStringCollection returned by the ConfigurationManager at runtime in an Asp.Net application?

I have tried the following but am told that the configuration file is readonly.

ConfigurationManager.ConnectionStrings.Add(new ConnectionStringSettings(params));

Is there another way to do this at runtime? I know at design time I can add a connection string to the web.config; however, I'm looking to add something to that collection at run time.

Thanks

EDIT: One of the reasons why I'm attempting to do this is due to a security requirement that prevents me from placing ConnectionStrings in the web.config (even encrypted). I would like to use elements like Membership and Profiles on my project; however, I am looking into an alternative to doing such w/o writing a custom provider. Custom Provider's aren't all that bad, but if I can find an easier solution, I'm all for it.

解决方案

var cfg = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration(@"/");
cfg.ConnectionStrings.ConnectionStrings.Add(new ConnectionStringSettings(params));

cfg.Save();

Be Advised this will cause your website to recycle since it modifies the config file. Check out http://msdn.microsoft.com/en-us/library/4c2kcht0(VS.80).aspx

这篇关于我可以添加到的ConnectionStrings在运行时ConnectionStringCollection?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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