从web抓取连接字符串.根据提供程序名进行配置

本文关键字:程序 配置 抓取 web 连接 字符串 | 更新日期: 2023-09-27 18:02:44

我想弄清楚如何从网络上抓取所有的连接字符串。配置有一个特定的提供商,但卡住了。

我的方法的基础是这样的

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"))
...

任何建议都是非常感谢的!

从web抓取连接字符串.根据提供程序名进行配置

已解决问题

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;
}