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

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

How to get Connection String From the Web.config

c#asp.netweb-config

提问by user559142

Possible Duplicate:
In C# , how can I read a connection string stored in my web.config file connection string?

可能重复:
在 C# 中,如何读取存储在我的 web.config 文件连接字符串中的连接字符串?

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;