Get multiple connection strings from web.config

How to get all connection string names from web.config through code in C #?

I tried this:

System.Configuration.Configuration webConfig = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("~"); ConnectionStringsSection x = webConfig.ConnectionStrings; string here = x.SectionInformation.Name; 
+8
c # web-config connection-string
source share
3 answers
  foreach (ConnectionStringSettings c in System.Configuration.ConfigurationManager.ConnectionStrings) { //use c.Name } 
+23
source share

Not defined for connection strings, but may be useful nonetheless.

 System.Web.HttpContext ctx = System.Web.HttpContext.Current; Configuration config; if (ctx != null) config = WebConfigurationManager.OpenWebConfiguration(ctx.Request.ApplicationPath); 
0
source share

Here's how to use LINQ to get a list of connection string names:

 List<string> names = ConfigurationManager.ConnectionStrings .Cast<ConnectionStringSettings>() .Select(v => v.Name) .ToList(); 

Or you can build his dictionary:

 Dictionary<string/*name*/, string/*connectionString*/> keyValue = ConfigurationManager.ConnectionStrings .Cast<ConnectionStringSettings>() .ToDictionary(v => v.Name, v => v.ConnectionString); 
0
source share

All Articles