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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1041853/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 06:48:11  来源:igfitidea点击:

C# connection string in web.config file

c#.netvisual-studio-2008web-configconnection-string

提问by flavour404

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

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

采纳答案by Kyle Sonaty

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

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

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

回答by Matthew Lock

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

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

要求您的项目引用 System.Web 和 System.Configuration。我实际上必须添加对“System.Configuration”的引用,而不仅仅是添加使用。