Reading connection string from external config fil

2020-02-10 14:25发布

问题:

I have created a console application and an app.config file and Connections.config file. The app.config file has a connectionstring property source pointing to the Connections.config

When I tried to read the connection string in the application, I get a ConfigurationErrorException

This is my main method.

static void Main(string[] args)
    {
        var settings = ConfigurationManager.ConnectionStrings;
        if (settings != null)
        {
            foreach (ConnectionStringSettings setting in settings)
            {
                Console.WriteLine(setting.ConnectionString);
            }
        }
    }

App.config file

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <connectionStrings configSource="Connections.config"></connectionStrings>
</configuration>

Connections.config file

<?xml version="1.0" encoding="utf-8" ?>
<connectionStrings>
  <add name="SQLDBConnecion"
   providerName="System.Data.ProviderName"
   connectionString="" />
</connectionStrings>

Here I observed two things. First: If I specify configSource I am unable to read the connection string (throwing exception.)

Second: If I put same connection string in App.config file and tried to read then the code is working but getting two connection string (which supposed to be return only one which is empty string) The first connection string is sqlexpress connection string like this

data source=.\SQLEXPRESS;Integrated Security=SSPI;
     AttachDBFilename=|DataDirectory|aspnetdb.mdf;User Instance=true

second connection string it returning is empty string (This is expected).

I want to read connection string from external file like in my scenario. How to do that? What am I missing here?

回答1:

MSDN says:

Do not include any additional elements, sections, or attributes.

You need to remove the XML encoding.

Edit

Also, you need to set the properties of your config file to Copy to Output Directory = Copy if newer or Copy always.

Edit 2

To build on what Dave said, you add the clear element to your external file. Your final Connections.config file should look exactly like this:

<connectionStrings>
  <clear/>
  <add name="Name"
     providerName="System.Data.ProviderName"
     connectionString="Valid Connection String;" />
</connectionStrings>


回答2:

Your Connections.config file should be as shown below without the xml header

<connectionStrings>
  <add name="SQLDBConnecion"
   providerName="System.Data.ProviderName"
   connectionString="" />
</connectionStrings>

Also for it to correctly locate the file in your console application, please set the Copy to Output Directory to Copy Always or Copy If Newer.



回答3:

That first connection string you are getting is inherited from the machine.config. This is described in the MSDN documentation. http://msdn.microsoft.com/en-us/library/bf7sd233(v=vs.90).aspx

You can use the Clear tag in your config file to remove inherited connection strings. http://msdn.microsoft.com/en-us/library/ayb15wz8(v=vs.90).aspx

<connectionStrings>
  <clear/>
  <add name="SQLDBConnecion"
   providerName="System.Data.ProviderName"
   connectionString="" />
</connectionStrings>


回答4:

There is a nice article on MSDN: https://msdn.microsoft.com/en-us/library/ms254494(v=vs.110).aspx.

Quote from the article:

To store connection strings in an external configuration file, create a separate file that contains only the connectionStrings section. Do not include any additional elements, sections, or attributes. This example shows the syntax for an external configuration file.

<connectionStrings>
  <add name="Name" 
   providerName="System.Data.ProviderName" 
   connectionString="Valid Connection String;" />
</connectionStrings>

Hope this helps people who run into this question later.