web.config文件中的C#连接字符串

时间:2009-06-25 02:06:15

标签: c# .net visual-studio-2008 web-config connection-string

在我的代码隐藏页面中,如何访问存储在web.config文件中的连接字符串?

3 个答案:

答案 0 :(得分:21)

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

这需要引用System.Configuration.dll和System.Web.dll。

答案 1 :(得分:4)

答案 2 :(得分:1)

来自对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。

相关问题