2

设置:

我有一个使用 ConnectionString 连接到 SQL 数据库的 DLL。当我将 DLL 添加到我的网站时,我必须将连接字符串添加到我的 web.config 中,以使我的 DLL 正常运行(这是设计使然)。一旦我将它添加到 web.config 中,那么一切正常(如预期的那样)。

问题:

当我想将连接字符串移动到在 IIS > 默认网站 > 属性 > ASP.NET 选项卡 > 编辑全局配置... > 连接字符串管理器中找到的网站的 ASP.NET 应用程序设置中时,问题就开始了

如果我将它从我的 web.config 中删除并将其放在那里,我的 DLL 将无法工作。如果我在我网站的其他任何地方(而不是来自 DLL)使用连接字符串,我可以通过这种方法很好地访问数据库,但由于某种原因,我的 DLL 只能在 web.config 中访问它。

问题:

如何让我的 DLL 使用 ASP.NET 配置设置连接字符串管理器而不是 web.config 中列出的连接字符串?

4

1 回答 1

3

您必须从通用 ASP.Net 配置设置中查找该部分,该部分可以通过 WebConfigurationManager 类检索,而不是通过 ConfigurationManager.ConnectionStrings 查找连接字符串。

    // Get the connectionStrings section.
ConnectionStringsSection connectionStringsSection =
    WebConfigurationManager.GetSection("connectionStrings")
    as ConnectionStringsSection;

// Get the connectionStrings key,value pairs collection.
ConnectionStringSettingsCollection connectionStrings =
    connectionStringsSection.ConnectionStrings;

// Get the collection enumerator.
IEnumerator connectionStringsEnum =
    connectionStrings.GetEnumerator();
于 2009-03-02T16:28:56.633 回答