How to change the connection string for different jobs

I am working on a C # 4.0, WPF 4.0, SQL 2008 project, and I work at home and in the office. I am just setting up SubVersion using Visual SVN in accordance with the recommendations contained in other questions. The problem I am facing is the connection string for the database for each location.

I have a database in my dev system, in the office the database is on our server. Neither on the Internet nor on the Internet, so I have to use both. Is there an elegant way to automatically choose the right one?

Update

I am having current problems with this, and I am trying to balance version control with getting work on my project. I read the demolition book and am well versed in what it covers. My only real problem is with the files, which should vary depending on the development environment. I could easily make my way around this, but it seems a little stupid to me. I see more than a few articles about how wacky svn: exclude can be, and it seems to me that what works at home causes problems at work and vice versa.

Maybe I just don’t know enough to find out the right answer, so please point me in the right direction (I don’t need you to do this for me) or vote for the best existing answer, and I will continue my research.

Thanks SO

+4
source share
7 answers

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

First save the settings for different environments in the original control, but not in the configuration file. For instance:

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 desired configuration file to the root app.config file. For instance. with a "pre-build event" (script command line) based on the "environment" parameters (computer_name, username, ...). You can probably achieve the same by adding a few msbuild commands to the .csproj file.

However, is all this really worth it? TortoiseSVN has a function called the ignore-on-commit list that helps prevent a locally modified file from being accidentally executed that should not be committed (in the commit dialog box, right-click the file => Move to change list → ignore-on- commit). It can be a little annoying if you really need to change something in the .config file, but still.

+4
source

Simple: do not put the connection string in the code; read it somewhere from the configuration data. And then just don't put this configuration data in Subversion.

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

+3
source

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

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

Such information should be in a configuration that you have better control over.

In addition, .NET supports encryption of the connection string section , if necessary.

+3
source

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

+1
source

In the source control, use the connection string in app.config, which is used for the most commonly used connection string (this will probably work).

At home, remove (or check) the file that selects which connection string to use, and edit it to use the connection’s home string.

0
source

You can use the settings to define multiple connection strings. just double-click Settings.settings from Solution Explorer-> project-> Properties. then you will see a combobox that contains the types of settings. select ConnectionString and enter connstr.

then you can get connstr with the code below

 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(); } } 

}

0
source

.NET has a way to override settings in one configuration file from another using the file attribute. Usually we 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 installed our version control system [SVN] to ignore any custom.config files. This allows us to check the basic parameters by default, while maintaining each developer can control the environment settings.

Note: this only works with the <appSettings> key. If you save connection strings in the <connectionStrings> key, consider migrating these parameters to appSettings.

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

0
source

Source: https://habr.com/ru/post/1300672/


All Articles