从ASP.NET应用程序更改web.config [英] Changing your web.config from your ASP.NET application

查看:64
本文介绍了从ASP.NET应用程序更改web.config的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在web.config文件中更改数据库连接字符串,但还没有如何进行此操作的线索。有人对此有任何经验吗?

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?

更新

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

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

推荐答案

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/zh-cn/library/system.configuration.configuration.connectionstrings.aspx

编辑:

http://msdn.microsoft.com/zh-cn/library/system.configuration.connectionstringssection.aspx

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

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.

祝你好运!

这篇关于从ASP.NET应用程序更改web.config的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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