C# connection string in web.config file

浪尽此生 提交于 2020-01-01 09:14:13

问题


In my code behind page, how do I access the connection string which is stored in my web.config file?


回答1:


System.Web.Configuration.WebConfigurationManager.ConnectionStrings["YouConnStringName"].ConnectionString;

This requires references to System.Configuration.dll and System.Web.dll.




回答2:


How to: Read Connection Strings from the Web.config File




回答3:


From comment on http://msdn.microsoft.com/en-us/library/ms178411.aspx

string c = System.Web.Configuration.WebConfigurationManager.ConnectionStrings["myconnectionstringname"].ConnectionString;

Requires your project to reference to System.Web and System.Configuration. I had to actually add a reference to "System.Configuration" not just add a Using.



来源:https://stackoverflow.com/questions/1041853/c-sharp-connection-string-in-web-config-file

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