Read connection string from web.config

How can i read connection string from web.config file in to a public class in class library. i've tried

WebConfigurationManager

ConfigurationManager

but these are not recognized in class library


Add System.Configuration as a reference.

For some bizarre reason it's not included by default.


您需要添加对System.Configuration的引用,然后使用:

System.Configuration.ConfigurationManager.
    ConnectionStrings["connectionStringName"].ConnectionString;

C#

// Add a using directive at the top of your code file    
using System.Configuration;

// Within the code body set your variable    
string cs = ConfigurationManager.ConnectionStrings["connectionStringName"].ConnectionString;

VB

' Add an Imports statement at the top of your code file    
Imports System.Configuration

' Within the code body set your variable    
Dim cs as String = ConfigurationManager.ConnectionStrings("connectionStringName").ConnectionString
链接地址: http://www.djcxy.com/p/42588.html

上一篇: web.config和app.config机器

下一篇: 从web.config读取连接字符串