SumitUpadhyay
SumitUpadhyay

Reputation: 21

c# connection string change due to database move

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??

Upvotes: 2

Views: 813

Answers (4)

ravibhagw
ravibhagw

Reputation: 1740

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:

  1. 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.

  2. Use a central XML configuration file that is parsed on application startup.

  3. 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.

Upvotes: 3

Aloraman
Aloraman

Reputation: 1420

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.

Upvotes: 0

LocEngineer
LocEngineer

Reputation: 2917

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/

Upvotes: 0

Venkatesan Rethinam
Venkatesan Rethinam

Reputation: 153

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.

Upvotes: 0

Related Questions