Changing your web.config from your ASP.NET application

萝らか妹 提交于 2019-12-22 08:37:21

问题


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


回答1:


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!




回答2:


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).



来源:https://stackoverflow.com/questions/3915845/changing-your-web-config-from-your-asp-net-application

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!