Switch connectiong string in web.config file base on configuration

G

Guest

How can I switch connection strings stored in config file based on the build
configuration? Or my real question is how most people maintain different
connection strings when their code is running in different environment, like
development, building, testing....

Thanks.

Daniel
 
N

Nicholas Paldino [.NET/C# MVP]

Daniel,

Well, personally, I would have two separate config files (just like you
have two separate binaries) which have two separate connection strings.

You could have a preprocessor statement for a constant which sets
different values based on the build type. However, that means you would
have to put both values into your config file. That's a little hokey to me,
and it's easier to just have two config files, IMO, as you don't want to
distribute the debug connection string in the release file.
 
G

Guest

Thanks Nicholas. I think your approach can work. But for me, it's a little
bit inconvenient, as our application is still growing quickly, the config
file changes all the time, lots of stuffs from the enterprise library. Keep
two or more config files be synchronized could be error prone. Can I just
seperate a little part of the config file and make two of them to adopt
different configurations?

Daniel
 
S

Steven Cheng[MSFT]

Hi Daniel,

Start from .net framework 2.0, most of the configuration sections in
application config file(app.config or web.config) supply a "configSource"
attribute. This attribute can let you separate the configure section of
that certain section out of the main configuration file. Thus, if you have
multiple configuration fragment (for that certain section) according to
different deployment scenario, you can only modify the separate file. Here
is a blog article introducing this feature:

#Using configSource to split configuration files
http://weblogs.asp.net/fmarguerie/archive/2007/04/26/using-configsource-to-s
plit-configuration-files.aspx

the following msdn document also mentioned this:

#SectionInformation.ConfigSource Property
http://msdn2.microsoft.com/en-us/library/system.configuration.sectioninforma
tion.configsource.aspx

Hope this helps some.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead


This posting is provided "AS IS" with no warranties, and confers no rights.
 
S

Steven Cheng[MSFT]

You're welcome!

Enjoy it! :)

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead


This posting is provided "AS IS" with no warranties, and confers no rights.
 
M

m.divyam

Thanks Nicholas. I think your approach can work. But for me, it's a little
bit inconvenient, as our application is still growing quickly, theconfig
file changes all the time, lots of stuffs from the enterprise library. Keep
two or moreconfigfilesbe synchronized could be error prone. Can I just
seperate a little part of theconfigfile and make two of them to adopt
different configurations?

Daniel

Hi Daniel,

i have another requirement. i need a config file in which i can define
appSettings.
but with the above one i am not able to do that..can you help me
regarding this.
if possible can you let me know how to define connection string
property also in the custom config file
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top