从 ASP.NET 应用程序更改 web.config

发布于 2024-09-27 18:33:18 字数 130 浏览 3 评论 0原文

我需要更改 web.config 文件中的数据库连接字符串,但不知道如何进行此操作。有人有这方面的经验吗?

更新
抱歉,上面不是很清楚,我希望能够在部署 Web 应用程序后更改连接字符串

I need to change the database connection string in the web.config file but havent got a clue how to go about this. Does anyone have any experience with that?

UPDATE
Sorry, not very clear above, I want to be able to change a connection string from the web application after it has been deployed

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

煞人兵器 2024-10-04 18:33:18
Configuration myConfiguration = WebConfigurationManager.OpenWebConfiguration("~");
//appSettings configuration
myConfiguration.AppSettings.Settings["xxx"].Value = "yyy";
//database connections configuration
myConfiguration.ConnectionStrings.ConnectionStrings["xxxconnection"].ConnectionString = "yyy";
myConfiguration.Save();

http://msdn.microsoft.com/en-us /library/system.configuration.configuration.connectionstrings.aspx

编辑:

http://msdn.microsoft.com/en-us/library/system.configuration.connectionstringssection.aspx

另外,正如其他人指出的那样,您需要设置正确的权限,但有时使用共享主机(根据经验),他们会要求您提供帐户的用户名和密码,一旦您输入,您的 web.config 就会更改。因此,请尝试一下,如果它不起作用并且您无权设置权限,那么恐怕您必须研究其他方法。

祝你好运!

Configuration myConfiguration = WebConfigurationManager.OpenWebConfiguration("~");
//appSettings configuration
myConfiguration.AppSettings.Settings["xxx"].Value = "yyy";
//database connections configuration
myConfiguration.ConnectionStrings.ConnectionStrings["xxxconnection"].ConnectionString = "yyy";
myConfiguration.Save();

http://msdn.microsoft.com/en-us/library/system.configuration.configuration.connectionstrings.aspx

Edit:

http://msdn.microsoft.com/en-us/library/system.configuration.connectionstringssection.aspx

Also, as pointed out by the others, you need to set the correct permissions but sometimes using shared hosting (from experience) they ask you for the username and password of your account and once you enter that, your web.config is changed. So try it and if it doesn't work and you don't have access to set the permissions then I'm afraid you have to look into something else.

Good luck!

指尖凝香 2024-10-04 18:33:18

使用 WebConfigurationManager 类,如下所示

由于它是非常敏感的信息,因此需要按照此网站(建议的链接)上的说明设置适当的权限作者:大卫·斯特拉顿)。

Use the WebConfigurationManager class as shown here.

As it is very sensitive information, proper permissions need to be set as explained on this site (link proposed by David Stratton).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文