猿问

从web.config读取连接字符串

如何从web.config文件读取连接字符串到类库中包含的公共类中?


我试过了:


WebConfigurationManager


ConfigurationManager

但是这些类在我的类库中无法识别。


智慧大石
浏览 636回答 3
3回答

翻翻过去那场雪

您需要添加一个引用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
随时随地看视频慕课网APP
我要回答