views:

358

answers:

7

I am working on a C# 4.0, WPF 4.0, SQL 2008 project and I do work at home and in the office. I just setup SubVersion using Visual SVN per the recommendations found in other questions. The problem I am having is the connection string for the database is different for each location.

At home I have the database on my dev system, in the office the database is on our server. Neither is exposed to the internet so I have to use both. Is there an elegant way to automatically select the correct one?

Update

I have been having ongoing issues with this and am trying to balance learning version control with getting work done on my project. I have been reading the subversion book and am fine with what it covers. My one real issue is dealing with files that need to vary between development environments properly. I could easily code my way around this but that seems a bit wacky to me. I do see more than a couple articles about how wacky the svn:exclude can be and it seems to me that what works at home is causing issues at work and vice-versa.

Perhaps I just don't know enough to recognize the right answer so please point me in the right direction (I don't need you to do it for me) or up vote the best existing answer and I will continue my research.

Thanks SO

+3  A: 

Simple: Don't put the connection string in code, read it from configuration data somewhere. And then just don't put that configuration data in Subversion.

In an app that I'm working on now, we store connection string information in the Windows registry, in HKLM\Software\[OurProduct]\Database.

Daniel Pryden
In general, I wouldn't recommend using the registry (not that it's necessarily evil). For things like connection strings, app.config will work nicely.
Aaron
@Aaron: Obviously, the registry isn't always the best choice. I merely offered it as one option. One advantage of using the registry is that it's easy to access from unmanaged code, including installers and other utilities. Basically, it comes down to how you want to package your application for deployment.
Daniel Pryden
+3  A: 

Can't you change the .config at home and not check the change to the connection string back in?

Holding connection string information in code is bad practice (anyone can use ildasm.exe and see all the strings in your code).

This kind of information should be in configuration which you have better control over.

Additionally, .NET supports encryption of the connection string section, if needed.

Oded
Why do you presume that the connection string is in web.config? In fact, why do you presume that the OP is talking about a web app? The question doesn't say ASP.NET.
Daniel Pryden
Fair enough. Dropped the `web` prefix...
Oded
I think this is what I was looking for. The string is currently stored in the Settings.settings file. I just excluded that file from SubVersion and I will see how that works when I get home tonight and do an update. This is my first day using any form of source control so I am still feeling the workflow out.
Mike B
Hmmm I cannot get this to work, the Settings.settings file works fine but I do not have the option to set svn:ignore on app.xaml. Instead of th eling list of properties there are only 5 options, none of which is ignore. I even tried adding it to the TortoiseSVN settings global ignore and it is still updated. Right now, after an update, I have to selectively roll back app.xaml to a different version.
Mike B
A: 

In source control, use the connection string in app.config that is used for the most commonly used connection string (likely to be work).

At home, hijack (or checkout) the file that selects which connection string to use, and edit it to use your home connection string.

Aaron
Why do you presume that the connection string is in web.config? In fact, why do you presume that the OP is talking about a web app? The question doesn't say ASP.NET.
Daniel Pryden
@Daniel: There is not enough of a difference between app.config and web.config for it to really matter whether the OP is trying to change a web app or a desktop app.
Brian
@Daniel: You are correct. In fact, I re-read the question and realized he was asking about a WPF application, which is definitely not ASP.NET. I have updated my post to reference 'app.config'. Thanks for pointing that out.
Aaron
A: 

you can use settings to define more than one connection string. just double-click Settings.settings from Solution Explorer->project->Properties. then you will see the combobox that contains types of settings. choose ConnectionString then enter the connstr.

then you can get the connstr with below code

using System.Configuration;

using test.Properties;

namespace test{
public partial class mainForm : Form{

   public mainForm()

    {
        InitializeComponent();            

    }

    private void mainForm_Load(object sender, EventArgs e)
    {
        string connectionStr = ConfigurationManager.ConnectionStrings[this_index_is_up_to your_algorithm].ToString();
    }
}

}

Aykut
+3  A: 

If you really want to automate this fully, you could do something like this:

First, store the settings for the different environments in source control, but not the actual configuration file. For example:

configfiles\app.config.mikeb_home
configfiles\app.config.local
configfiles\app.config.development
configfiles\app.config.staging
configfiles\app.config.production

Then in your build configuration, you can add a step to copy the right config file to your root app.config. E.g. with a 'pre-build event' (command line script) based on 'environment' parameters (computername, username, ...). You can probably achieve the same thing by adding some msbuild commands in the .csproj file.

However, is all this really worth it? TortoiseSVN has a feature called the 'ignore-on-commit' list, which helps you prevent accidently committing a locally changed file that shouldn't be committed (in the commit dialog, right-click on the file => Move to Change List -> ignore-on-commit). May be slightly annoying if you actually need to change something else in the .config file, but still.

jeroenh
I think that did the trick, I will know when I get home (Using the ignore on commit list) For some reason I could not set that property on the file in the VS solution explorer or using TortoiseSVN from windows explorer but I was able to by making a change to the file (I added a comment) then changing it in the commit dialogThanks!
Mike B
A: 

.NET has a way of overriding settings in one config file from those in another using the file attribute. We usually do something like this:

web.config

<configuration>
    <appSettings file="web.custom.config">
        <!-- your default settings here -->
    </appSettings>
</configuration>

web.custom.config

<appSettings>
    <!-- override stuff from root web.config here here -->
</appSettings>

By convention, we set up our source control system [SVN] to ignore any custom.config files. This allows us to check-in the core, default settings while still allowing each developer to manage environment-specific settings.

Note: this only works with the <appSettings> key. If you're storing connection strings in the <connectionStrings> key, consider moving those settings to appSettings instead.

See http://msdn.microsoft.com/en-us/library/ms228154.aspx

Seth Petry-Johnson
+1  A: 

I think the ideal solution would be to use something like the web.config transforms that are being added in Visual Studio 2010. Unfortunately as far as I can tell these are only available for web.config files.

dlannoye