Normally, in a .NET application, you store your database connection strings in the app.config.
For a single .exe, there's a single .exe.config. And we're using Entity Framework, which defaults to using a .config setting that matches the DbContext name. Which all works fine, so long as you follow the common pattern of having a single instance of an application talking to a single database.
But our problem requires we have multiple instances of our application, each with its own distinct database connection.
Possible solutions:
Create multiple copies of the .exe, each with its own copy of the .config, and put a different connection string in each. Advantage? No coding changes. Disadvantage? Maintaining all the different copies, making sure they all get updated, etc.
Keep one copy of the .exe, with one copy of the .config, and put multiple connection strings in the .config. Then pass a command-line argument to the program that selects which connection string to use. Advantage? Only one .exe to manage. Disadvantage? Coding change, likelihood that someone's going to mess up the command-line arguments.
Some other brilliant idea, that someone here on Stack Overflow is going to point out to me.
Any ideas?
I'm sure people have dealt with this issue, before. Is there a common way of handling the issue?
You can also think of creating copies of config file and load the appropriate one based on command line parameter using following code:
AppDomain.CurrentDomain.SetData ("APP_CONFIG_FILE", "path to config file")
See this for detail: Relocating app.config file to a custom path
You could try something like this within some C# applications to change the connection string per application.
var config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
var connectionStringSection = (ConnectionStringsSection)config.GetSection("connectionStrings");
connectionStringSection.ConnectionStrings["database"].ConnectionString = string.Format("Data Source={0}", userCon);
config.Save();
ConfigurationManager.RefreshSection("connectionStrings");
Related
We are moving database from one server to another. There are many connection strings of applications that needs to changed due to this. Is there any generic way where we can keep the connection string so that if we move the database again then this issue doesn't arise??
There are so many ways to resolve this problem.
Ultimately what it sounds like you want to do is centralize your database connection strings in such a way that a database migration is (mostly) transparent to your application. I can think of a few options here:
Use a "control database" that houses your connection strings and configurations. If you migrate to a new database server, you only have to update a single connection string in your application, and perform data updates for everything else. This is what I personally use.
Use a central XML configuration file that is parsed on application startup.
Use SQL Server Aliases and/or add additional IPs to the machine that you can migrate between servers. This way when you move to a new database server you can still bring along the existing aliases/IPs to that server (unless they need to run in parallel of course) and theoretically not need to update anything in your code, provided that you've referenced the appropriate aliases. See here for more info: https://dba.stackexchange.com/questions/56642/how-to-create-a-server-alias-in-sql-server-2012
Over here we ultimately went with option 1 because that gave us more flexibility and reliability than an XML configuration file (#2) and required less configuration and special cases to be documented with our DBAs, since we have no ownership of any database servers outside of our DEV environment.
Have the connection string in an xml file and read the xml file and get the connection string from it. You only need to change the connection string in the xml file and not in the published code. By the way you can have multiple connection strings for Debug and Release modes and make your application choose it.
You could store them in a database. ^_^
Jokes aside, I do not have experience with this myself, but using an Alias sound like it might suit you best:
http://blog.idera.com/sharepoint/performance-webcasts/plan-your-sharepoint-farm-right-with-a-sql-server-alias/
First, you can store your connection strings in external file (custom storage or .config file) - so you will be able to change them without recompiling the code.
Second, you can use domain name instead of IP address
And third, you can store your connection strings in parts and build them in runtime by ConnectionStringBuilder - so you can change only server part.
I've read a few links but most are in relation to when deploying or i've missed a trick in the middle so dont fully understand hence asking the below question......
I have a live, test and local environment (all have their own individual connection strings). I create a solution with 2 projects (for example) 1 is a ASP .Net website and other is a Class Library. Within the class library i add a Linq to SQL and add a connection to it. The connection string is stored in an app.config file.
What i would like to do is be able to switch between the environments so the connection string is updated to reflect the environment i am using without having to manually type in the connection string. I've seen this done but not sure how to do it myself? Im using VS2010. Could anyone advise or point me in the right direction?
Thanks
You could potentially use #if DEBUG and have two connections strings in your app.config - one called Test one called Live - I would suspect that debugging would indicate that you are in the test environment and on release you would be live.
Please refer to this link http://msdn.microsoft.com/en-us/library/4y6tbswk.aspx
Another option is to simply have an input at some point to indicate which environment the application is running on - you could do this with a checkbox or radio button or even through appSettings in app.config by specifying ConnectionMode and setting it to 1 or 2 if the setting reads 1, then use the test connection string, if it reads 2 Live connection string.
We have a large VS solution, consisting of many projects representing the various model, service and presentation layers.
Currently the connection string to the main database is spread throughout the solution. On the web front end, its contained in the web.config file. The data initialization and web forms app have it contained in separate nhibernate.config files.
Is there a better way to manage the connection string?
I was thinking if somehow it was centralized in one point, it would then be easier to manage and update, rather than having it spread all over the place.
If the intention is to have only one place in the solution where the connection string need be specified, then the following should work.
Split out the connection strings section into a separate file and reference it in the configuration files that you need to using
<connectionStrings configSource="connectionStrings.config"/>
Now put connectionStrings.config file in a common location (perhaps Solution Items). Add it as a link to each project that requires it using Add Existing Item -> Add as Link.
Because all of the projects now have a link to the one connectionStrings.config file, any changes to that file should propagate to the linked items in each project in the solution.
My Windows service should connect to different Databases and get the data from that (multitenent). and each connection maintain in seperate thread. Is this possible?
Database information is available in XML file.
Does anyone have a suggestion about how this can be done?
Windows services run all of the time and as you have the database details contained within a config file of sorts then you can read the connection string from there.
When structuring the code, a service is no different from a console program, other than those bits necessary for the ServiceManager to pick up on.
So structure the code such that the main logic is database agnostic i.e. it doesn't care whats beyond the datalayer interface only that it knows what the interface is.
Check out Repository and factory patterns. These will be of help. You can then create a concrete instance of the correct database class at runtime, by reading what the config file says.
Remember that the service will always run (unless set to be manually triggered) and have it be able to fail quietly if it doesn't find the config file.
I have used Ruby on Rails with ActiveRecord, so I am quite used to switching to Production/Development database.
I am wondering, how do people implement Development and Production Database difference in ASP.NET MVC (preferably with ado.net entity).
I tried to have it by creating 2 entity datasets with the same name in a different namespace, so I would only need to switch the used namespace in my database code. Unfortunately, this didn't work because the entity datasets do not allow equal names...
The way I do it (with a web app) is to have separate versions of the Web.config file for each of Development (web.config), QA (web-qa.config), and Production (web-prod.config) installs. The config files have different connection strings pointing to the corresponding instances of the database. The default connection string points to the SQL server instance on my local machine which isn't available to the network.
As part of the publishing step, I manually delete the web.config file and rename the appropriate QA/Prod configuration to web.config. This hooks up that instance of the app to the correct instance of the database.
The easiest way (though it's still not as easy as in Rails) will be to include connection strings into App.config.
You should have one entity dataset, and instatiate the entities object using the constructor that takes a ConnectionString, which you can read in from:
WebConfigurationManager.ConnectionStrings["ConnStrName"].ConnectionString;
The ConnectionStrings element also supports the general configSource attribute.
You can therefore have something like:
<connectionStrings configSource="PathToConnectionStrings.config" />
This allows you to have all the connection strings held in a seperate file, that can then have different values on each environment.
Note that unlike the appSettings file attribute, this doesn't do a merge, it completely replaces the section.
Note that in VS2010, you will have the facility to modify the .config based on the build configuration, so could change the values just by setting your build to "Production" say.