C# 如何从 Web.config 获取连接字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9412361/
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
How to get Connection String From the Web.config
提问by user559142
Possible Duplicate:
In C# , how can I read a connection string stored in my web.config file connection string?
I am using the SqlConnection class to connect to a sql server database. One of the constructors for this class required a connection string argument...What is the easiest way to get the required information from the web.config file....
我正在使用 SqlConnection 类连接到 sql server 数据库。此类的构造函数之一需要连接字符串参数...从 web.config 文件获取所需信息的最简单方法是什么....
采纳答案by adatapost
Use ConfigurationManager.ConnectionStringscollection to read connection string from web.config.
使用ConfigurationManager.ConnectionStrings集合从 web.config 读取连接字符串。
回答by Vinod
Try this:
尝试这个:
First include this Namespace using System.Configuration;
首先包含这个命名空间 using System.Configuration;
and use this
并使用这个
string cnn = ConfigurationManager.ConnectionStrings["conn"].ConnectionString;
回答by Ghost Answer
connection string in web config
Web 配置中的连接字符串
import this
导入这个
using System.Configuration;
使用 System.Configuration;
in aspx page
在aspx页面
SqlConnection con;
string conn = ConfigurationManager.ConnectionStrings["connection string name in web config"].ConnectionString;
SqlConnection con;
string conn = ConfigurationManager.ConnectionStrings["connection string name in web config"].ConnectionString;

