[英]Get multiple connection strings from web.config
How to get all the connection strings's names from the web.config via code in C#? 如何通过C#中的代码从web.config获取所有连接字符串的名称?
I tried this: 我试过这个:
System.Configuration.Configuration webConfig = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("~");
ConnectionStringsSection x = webConfig.ConnectionStrings;
string here = x.SectionInformation.Name;
foreach (ConnectionStringSettings c in System.Configuration.ConfigurationManager.ConnectionStrings)
{
//use c.Name
}
This is how to use LINQ to get list of connection string names: 这是如何使用LINQ获取连接字符串名称列表:
List<string> names = ConfigurationManager.ConnectionStrings
.Cast<ConnectionStringSettings>()
.Select(v => v.Name)
.ToList();
Or you can build a dictionary of it: 或者您可以构建它的字典:
Dictionary<string/*name*/, string/*connectionString*/> keyValue = ConfigurationManager.ConnectionStrings
.Cast<ConnectionStringSettings>()
.ToDictionary(v => v.Name, v => v.ConnectionString);
Not specific to Connection Strings, but might be helpful none the less. 不是特定于连接字符串,但可能会有所帮助。
System.Web.HttpContext ctx = System.Web.HttpContext.Current;
Configuration config;
if (ctx != null)
config = WebConfigurationManager.OpenWebConfiguration(ctx.Request.ApplicationPath);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.