In the project that I'm currently working I had a problem because my connection strings are different from the other developers: I want to work locally in my SQL Express so the connections strings are different and since we are developing with Visual Source Safe 2005 as the version control system it is not possible to change the file where the connections strings are stored.

The easiest way was not to keep the web.config file under source control but of course that wasn't the desired way because we have lots of settings that we want to keep under source control.

After googling a while I found that the appSettings section in the web.config file of an ASP.NET web application has an attribute – file – that can point to an external file where we can store additional settings.

So my initial web.config file is:

<appSettings

<add
key="DB_1_ConnectionStringName"
value="DB_1" />

<add
key="DB_2_ConnectionStringName"
value="DB_2" />

</appSettings>

 

<connectionStrings>

<add
name="DB_1"
connectionString="server=db_server;database=db_1; … "
providerName="System.Data.SqlClient" />

<add
name="DB_2"
connectionString="server=db_server;database=DB_2; … "
providerName="System.Data.SqlClient" />

</connectionStrings>

Of course, when I want to work locally in my SQL Express instance this doesn't work. So I changed the web.config file like this:

<appSettings file="custom.config"

<add
key="DB_1_ConnectionStringName"
value="DB_1" />

<add
key="DB_2_ConnectionStringName"
value="DB_2" />

</appSettings>

 

<connectionStrings>

<add
name="DB_1"
connectionString="server=db_server;database=db_1; … "
providerName="System.Data.SqlClient" />

<add
name="DB_2"
connectionString="server=db_server;database=DB_2; … "
providerName="System.Data.SqlClient" />

 

<add
name="DB_1_LOCAL"
connectionString="server=.\SQLEXPRESS;database=db_1; … "
providerName="System.Data.SqlClient" />

<add
name="DB_2_LOCAL "
connectionString="server=db_.\SQLEXPRESS;database=DB_2; … "
providerName="System.Data.SqlClient" />

 

</connectionStrings>

And my custom.config file is:

<appSettings

<add
key="DB_1_ConnectionStringName"
value="DB_1_LOCAL" />

<add
key="DB_2_ConnectionStringName"
value="DB_2_LOCAL" />

</appSettings>

 

What happens is that the settings in the custom.config file override the settings in the web.config file.

The other developers don't need to have the custom.config files because "the runtime ignores the attribute, if the specified file cannot be found" [msdn].

So I have only the custom.config file that it isn't under source control but that's no problem because I only redefine the name of the connection strings. The actual connection strings are still stored in the web.config file and under source control.

 

LEAVE A REPLY

Please enter your comment!
Please enter your name here