【发布时间】:2010-11-05 17:24:18
【问题描述】:
在我的后台代码中,如何访问存储在我的 web.config 文件中的连接字符串?
【问题讨论】:
标签: c# .net visual-studio-2008 web-config connection-string
在我的后台代码中,如何访问存储在我的 web.config 文件中的连接字符串?
【问题讨论】:
标签: c# .net visual-studio-2008 web-config connection-string
System.Web.Configuration.WebConfigurationManager.ConnectionStrings["YouConnStringName"].ConnectionString;
这需要引用 System.Configuration.dll 和 System.Web.dll。
【讨论】:
Cannot apply indexing with [] to an expression of type 'System.Configuration.ConnectionStringSettingsCollection' 任何想法如何解决这个问题?
来自http://msdn.microsoft.com/en-us/library/ms178411.aspx的评论
string c = System.Web.Configuration.WebConfigurationManager.ConnectionStrings["myconnectionstringname"].ConnectionString;
要求您的项目引用 System.Web 和 System.Configuration。我实际上必须添加对“System.Configuration”的引用,而不仅仅是添加一个 Using。
【讨论】: