Jeff
Jeff

Reputation: 14279

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#

Upvotes: 2

Views: 1753

Answers (5)

Ali Reza Pooneh
Ali Reza Pooneh

Reputation: 51

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;

Upvotes: 0

KBBWrite
KBBWrite

Reputation: 4409

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.

Upvotes: 1

Lloyd
Lloyd

Reputation: 2942

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

Upvotes: 1

user44298
user44298

Reputation: 900

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])

Upvotes: 2

Conrad Frix
Conrad Frix

Reputation: 52675

have you tried

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

Upvotes: 1

Related Questions