简体   繁体   中英

Differentiate between ConnectionStrings in the machine.config and web.config

Using C#, is there are way to differentiate between ConnectionStrings in the machine.config and the web.config? I would like to iterate over the collection in the web.config but not those from the machine.config.

ASP.NET 3.5, C#

Try the code bellow or issue linq a query to find the compliment(differences) of both configs. The following yeilds true if connection string at index 0 is coming from the machine config where it also compares the connection string at index 0 otherwise yields false:

System.Configuration.ConfigurationManager.ConnectionStrings[0].Equals
(System.Configuration.ConfigurationManager.OpenMachineConfiguration()
.ConnectionStrings.ConnectionStrings[0])

你有没有尝试过

Configuration c = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("/");

From MSDN, also look at the System.Web.Configuration namespace.

How to: Read Connection strings from the Web.config file

System.Configuration.Configuration rootWebConfig =
            System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("/MyWebSiteRoot");
        System.Configuration.ConnectionStringSettings connString;
        if (rootWebConfig.ConnectionStrings.ConnectionStrings.Count > 0)
        {
            connString =
                rootWebConfig.ConnectionStrings.ConnectionStrings["NorthwindConnectionString"];
            if (connString != null)
                Console.WriteLine("Northwind connection string = \"{0}\"",
                    connString.ConnectionString);
            else
                Console.WriteLine("No Northwind connection string");
        }

For get first ConnectionString in localweb.config, use it:

 var ms = System.Configuration.ConfigurationManager.OpenMachineConfiguration();
 if (ConfigurationManager.ConnectionStrings.Count > ms.ConnectionStrings.ConnectionStrings.Count)
            return ConfigurationManager.ConnectionStrings[ms.ConnectionStrings.ConnectionStrings.Count].ConnectionString;

Every configuration in the Web.Config can also be put in machine.config. You can think of Machine.Config is the base class and the Web.Config is the sub class.

So if you override any settings in Web.Config, you are basically overriding the machine configuration settings (Or asking the application to use the web.config settings)

So I think if you write connections sting in Web.Config, then from your application when you loop through the connectionstrings

ConfigurationManager.ConnectionStrings

you will be able to access only the connection strings that you have written in web.config.

Please try the <clear/> in the web.config connectionstring section. So I think that will clear the Machine.config connction strings.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM