简体   繁体   English

区分machine.config和web.config中的ConnectionString

[英]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? 使用C#,有没有办法区分machine.config和web.config中的ConnectionString? I would like to iterate over the collection in the web.config but not those from the machine.config. 我想遍历web.config中的集合,而不要遍历machine.config中的那些。

ASP.NET 3.5, C# ASP.NET 3.5,C#

Try the code bellow or issue linq a query to find the compliment(differences) of both configs. 尝试下面的代码或向linq查询以查找两个配置的互补性(差异)。 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: 如果索引0处的连接字符串来自机器配置,并且它也比较索引0处的连接字符串,则以下条件为true:否则产生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. 在MSDN中,还要查看System.Web.Configuration命名空间。

How to: Read Connection strings from the Web.config file 如何:从Web.config文件读取连接字符串

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: 要在localweb.config中获取第一个ConnectionString,请使用它:

 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. Web.Config中的每个配置也可以放在machine.config中。 You can think of Machine.Config is the base class and the Web.Config is the sub class. 您可以认为Machine.Config是基类,而Web.Config是子类。

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) 因此,如果覆盖Web.Config中的任何设置,则基本上是覆盖计算机配置设置(或要求应用程序使用web.config设置)

So I think if you write connections sting in Web.Config, then from your application when you loop through the connectionstrings 所以我认为,如果您在Web.Config中编写连接字符串,那么当您遍历连接字符串时从应用程序中获取连接字符串

ConfigurationManager.ConnectionStrings

you will be able to access only the connection strings that you have written in web.config. 您将只能访问在web.config中编写的连接字符串。

Please try the <clear/> in the web.config connectionstring section. 请尝试在web.config connectionstring部分中的<clear/> So I think that will clear the Machine.config connction strings. 因此,我认为这将清除Machine.config连接字符串。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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