根据提供者名称从 web.config 获取连接字符串

发布于 2024-11-24 05:27:23 字数 502 浏览 1 评论 0原文

我试图弄清楚如何从具有特定提供商但卡住了的 web.config 中获取所有连接字符串。

我的方法的基础看起来像这样

var webConfig = new ExeConfigurationFileMap { ExeConfigFilename = GlobalSettings.FullpathToRoot + "web.config" };
var config = ConfigurationManager.OpenMappedExeConfiguration(webConfig, ConfigurationUserLevel.None);
var section = (ConnectionStringsSection)config.GetSection("connectionStrings");

// some code like 
foreach(var item in section.providerName.equals("sql.provider"))
...

非常感谢任何建议!

Im trying to figure out how to grab all connectionStrings from a web.config that has a certain provider but got stuck.

The basics of my method looks like this

var webConfig = new ExeConfigurationFileMap { ExeConfigFilename = GlobalSettings.FullpathToRoot + "web.config" };
var config = ConfigurationManager.OpenMappedExeConfiguration(webConfig, ConfigurationUserLevel.None);
var section = (ConnectionStringsSection)config.GetSection("connectionStrings");

// some code like 
foreach(var item in section.providerName.equals("sql.provider"))
...

Any advice is highly appreciated!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

你曾走过我的故事 2024-12-01 05:27:23

解决了这个问题

public static Dictionary<string,string> ExistingConnections()
{
    var connections = new Dictionary<string, string>();
    foreach (ConnectionStringSettings connection in ConfigurationManager.ConnectionStrings)
    {
        if(connection.ProviderName == "sql.providername")
            connections.Add(connection.ProviderName, connection.ConnectionString);
    }
    return connections.Count > 0 ? connections : null;
}

或者使用linq

public static Dictionary<string,string> ExistingConnections()
{
    var connections = ConfigurationManager.ConnectionStrings.Cast<ConnectionStringSettings>().Where(connection => connection.ProviderName == "Camelot.SharePointProvider").ToDictionary(connection => connection.ProviderName, connection => connection.ConnectionString);
    return connections.Count > 0 ? connections : null;
}

Solved the issue

public static Dictionary<string,string> ExistingConnections()
{
    var connections = new Dictionary<string, string>();
    foreach (ConnectionStringSettings connection in ConfigurationManager.ConnectionStrings)
    {
        if(connection.ProviderName == "sql.providername")
            connections.Add(connection.ProviderName, connection.ConnectionString);
    }
    return connections.Count > 0 ? connections : null;
}

or by using linq

public static Dictionary<string,string> ExistingConnections()
{
    var connections = ConfigurationManager.ConnectionStrings.Cast<ConnectionStringSettings>().Where(connection => connection.ProviderName == "Camelot.SharePointProvider").ToDictionary(connection => connection.ProviderName, connection => connection.ConnectionString);
    return connections.Count > 0 ? connections : null;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文