views:

378

answers:

1

Hi guys I'm doing a c# course at university, and now I'm beginning linQ to xml, linQ to Sql-Server, etc. I work with the example projects in two PCs (university PC and office PC) Is there an easy way to change connection string (from app.config) at runtime, or designtime (using a constant in main.cs) so I can use a connection string at university, and a connection string at the office easily?

Thanks a lot in advance,

+3  A: 

You could try something like:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <appSettings>
    <add key="Environment" value="Home"/>
  </appSettings>
  <connectionStrings>
    <add name="Work" connectionString="..."/>
    <add name="Home" connectionString="..."/>
  </connectionStrings>
</configuration>

and, later:

string environment = ConfigurationManager.AppSettings["Environment"];
ConfigurationManager.ConnectionStrings[environment].ConnectionString;
Rubens Farias
That's very similar to how I do it, but I grab the computer name and based off that determine if its DEV,STAGE,PROD,QA etc.. same thing basically.
Jack Marchetti
Actually, I comment out lines I don't want; of course, production configurations doesn't exist on development environment
Rubens Farias
Great tip! Thanks!
Enrique
For code to compile, I had to move <appSettings> <add key="Environment" value="Home"/> </appSettings> below </connectionStrings>
Enrique