2

我创建了一个 Web 用户控件来操作我的数据库操作,并且我想使用存储在 web.config 中的连接字符串 .... 我以前在 asp.net 页面中使用的方式(如 deafult.aspx.cs)是添加

using System.Web.Configuration;

然后使用下面的代码:

private string connectionString =
             WebConfigurationManager.ConnectionStrings["Pubs"].ConnectionString;

问题是我无法添加using System.Web.Configuration;到我的 WebUserControl ... 是否可以通过 webUserControl 从 Web.Config 读取数据?

4

1 回答 1

1

You probably just have to add the assembly reference for System.Configuration and you can then use

private string connectionString = 
    ConfigurationManager.ConnectionStrings["Pubs"].ConnectionString;

It will always read the connectionstring from web.config (and not from the app.config of the project as you might think). If you want to use the WebConfigurationManager, you will have to add a reference to the assembly System.Web, but that should be already there in a web control library project

于 2012-12-22T10:49:11.193 回答