1

Good Day,

I have two connection strings defined in my web.config file.

<connectionStrings>
    <add name="AppDb" connectionString="CONNECTION-STRING1"/>
    <add name="LaptopDb" connectionString="CONNECTION-STRING2" />
</connectionStrings>

When I am working on my desktop, I want to use the connection string "AppDb". When I am working on my laptop, I want to use the connection string "LaptopDb". I don't want to comment out the line on the connection string everytime I work on a different machine.

I know that I can programatically do this. I'm just trying to figure out the best way.

Something like:

if (machineName == desktop)
     use AppDb
else
     use LaptopDb

but I don't like this approach. Is there something else I can test on?

2 Answers 2

1

Here's another couple of approaches you could consider:

Sign up to request clarification or add additional context in comments.

Comments

1

Really not too hard to do -- the trick is to use the System.Environment.MachineName to drive which string to pick and to get your connection string from a static property:

public static string ConnectionStringName
{
    get
    {
        var customConnection = ConfigurationManager.ConnectionStrings[Environment.MachineName] != null;
        var connectionStringName = customConnection ? Environment.MachineName : "DefaultDb";
        return connectionStringName;
    }
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.