Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / September 2007

Tip: Looking for answers? Try searching our database.

Switch connectiong string in web.config file base on configuration

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Daniel - 23 Aug 2007 09:24 GMT
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
Nicholas Paldino [.NET/C# MVP] - 23 Aug 2007 16:10 GMT
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.

Signature

         - Nicholas Paldino [.NET/C# MVP]
         - mvp@spam.guard.caspershouse.com

> How can I switch connection strings stored in config file based on the
> build
[quoted text clipped - 6 lines]
>
> Daniel
Daniel - 24 Aug 2007 02:22 GMT
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
Steven Cheng[MSFT] - 24 Aug 2007 04:14 GMT
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.
Daniel - 24 Aug 2007 04:20 GMT
Thanks Steven, that's what I want.

Daniel
Steven Cheng[MSFT] - 24 Aug 2007 04:57 GMT
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.divyam@gmail.com - 11 Sep 2007 10:26 GMT
> 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
[quoted text clipped - 4 lines]
>
> 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

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.