从web.config文件中读取值?

时间:2012-10-23 11:55:46

标签: c# asp.net web-config

从我的DAL,如何从web.config中读取连接字符串?

3 个答案:

答案 0 :(得分:4)

您可以使用WebConfigurationManager从web.config

读取连接字符串值

代码示例:

using System.Configuration;

string connString = WebConfigurationManager.ConnectionStrings["ConnectionString"].ToString();

确保已将System.configuration引用添加到DAL。

答案 1 :(得分:2)

using System.Configuration;

static string connectionString = 
    ConfigurationManager.ConnectionStrings["MyConn"].ConnectionString;

如果您的项目中有多个Web.Config文件使用数据连接字符串,则Yuu只需要WebConfigurationManager

What's the difference between the WebConfigurationManager and the ConfigurationManager?

答案 2 :(得分:0)

System.Configuration.ConfigurationManager.ConnectionStrings["connStr"].ConnectionString