可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
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
回答1:
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.
回答2:
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.
回答3:
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.
回答4:
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.
回答5:
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();
}
}
}
回答6:
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.
回答7:
.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