if statement in web.config file

S

sck10

Hello,

Currently, I have two web.config files on different machines (test and
production). The only difference between the two is that the
connectionstring and appsettings point to different data sources (testdb and
productiondb). Is there a way to use something like an if statement or case
statement in a web.config file to determine which connection string or app
settings to use depending on the machine name?
--
Thanks in advance,

sck10

Data Source # 1
------------------------
<configuration>
<connectionStrings>
<add
name="cnnSQL"
connectionString="Data Source=DataSource01;
Persist Security Info=True;
Initial Catalog=MyDB;
User ID=MyUser;
Password=MyPswd"
providerName="System.Data.SqlClient" />
</connectionStrings>

<appSettings>
<add
key="cnnSQL"
value="Server=DataSource01;
User ID=MyUser;
Password=MyPswd;
Database=MyDB;
Persist Security Info=True;
Network Library=dbmssocn"/>
</appSettings>


Data Source # 2
------------------------
<configuration>
<connectionStrings>
<add
name="cnnSQL"
connectionString="Data Source=DataSource02;
Persist Security Info=True;
Initial Catalog=MyDB;
User ID=MyUser;
Password=MyPswd"
providerName="System.Data.SqlClient" />
</connectionStrings>

<appSettings>
<add
key="cnnSQL"
value="Server=DataSource02;
User ID=MyUser;
Password=MyPswd;
Database=MyDB;
Persist Security Info=True;
Network Library=dbnmpntw"/>
</appSettings>
 
R

Ray Costanzo [MVP]

Where I work, we using something like:

<add "sqlDev" connectionString="...dev sql connection string" />
<add "sqlTest" connectionString="...dev sql connection string" />
<add "sqlProd" connectionString="...dev sql connection string" />


Then, use application logic in the code to determine which connection string
the app should use.

You can't put the logic directly in the .config file, so I'd say you'll
either have to maintain separate config files (and hope no one accidentally
uses a test one in production for a month before anyone notices) or put the
logic in your app code.

Ray at work
 
J

Jason Hales

That sort of thing isn't possible.

Is your code doing different things depending on the value of the
connection string?

The only time I've done something similar is using #if statements to do
additional steps if I'm in a debug build,ie using #if (DEBUG)
 
S

Steven Cheng[MSFT]

Hi sck10,

As for the configuration setting/sections, in .net 2.0, most configuration
sections provide the "configSource" attribute that can help us separate a
certain configurationSection's setting into a separate external file(other
than the application config file). So in your scenario, you can consider
provide two version of such external files for your <appSettings> and
<connecitonStrings> ...

#appSettings Element (General Settings Schema)
http://msdn2.microsoft.com/en-us/library/ms228154(VS.80).aspx

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

Hope this also helps.

regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
 

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

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top