Simon
Simon

Reputation: 1333

Reference connection strings in separate config file

I would like to move my connection strings from my web.config file to a file called ConnectionStrings.config, both files are in the root of my web application.

Web.Config

<configuration>
    <connectionStrings configSource="ConnectionStrings.config"/>
</configuration>

ConnectionStrings.Config

<configuration>
    <connectionStrings>
        <add name="myConnectionString" connectionString="*****" providerName="System.Data.SqlClient" />
    </connectionStrings>
</configuration>

But when I try and run this I get the following error

The format of a configSource file must be an element containing the name of the section. (C:\Your Application\ConnectionStrings.config line 2)

Any ideas?

Upvotes: 3

Views: 3116

Answers (1)

Baral
Baral

Reputation: 3141

Web.Config

 <connectionStrings configSource="ConnectionStrings.config" />

ConnectionStrings.config:

<?xml version="1.0" encoding="utf-8" ?>
<connectionStrings>
    <add name="name" connectionString="server=(local);database=db;user Id=usr;password=pass;timeout=0"></add>
</connectionStrings>

This is how (without the providerName) I do it and it works fine.

Upvotes: 9

Related Questions