We're a community of 1077K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,076,167 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

Changing connectionstring (preferably only database location) at runtime?

Hi!
I need to be able to change the location of the database at runtime through a menu in my program. My question is how to best achieve this.
If for example the ip to the server change, the user need to be able to update the database path; i.e. change a part of the connectionString.
This would be easy to achieve hardcoding the connectionString into the code but I'm not sure if that is the right thing to do since it contains a user and password.

Currently I'm storing the connectionString into an app.config file that I've encrypted. Is there a way to change only the location of the database in this file?
I could of course remove the connectionString from the .config file replacing it with an updated one at runtime but then again I would need to write the user and password into the code.
Or should I use Integrated Security instead and write the connectionString into the code instead of the .config file.

Im using Linq to Sql with a MSSql database.

3
Contributors
3
Replies
3 Hours
Discussion Span
2 Years Ago
Last Updated
4
Views
Question
Answered
Timmo987
Newbie Poster
2 posts since Jun 2010
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

If you are going to change the path of the database that you have change the connection string and reopen the connection, so how it is possible without entering the password or without storing some where in your program.

One of the way to accomplish your task is use the class file, create a class and store the user name and password in a variable once and then use, just change the path when ever require and reopen the connection

Feel free to ask if any query ......
Best Of Luck

prvnkmr194
Posting Whiz in Training
222 posts since Oct 2010
Reputation Points: 11
Solved Threads: 44
Skill Endorsements: 0

Each connection type has a class for building connection strings, such as the SqlConnectionStringBuilder. You can put all the parts you need into your app.config like you have now and then build your string. If the user needs to change part of it, you just change that part.

Momerath
Senior Poster
3,729 posts since Aug 2010
Reputation Points: 1,322
Solved Threads: 624
Skill Endorsements: 13

Thank you. SqlConnectinStringBuilder will probably work.

Timmo987
Newbie Poster
2 posts since Jun 2010
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0
Question Answered as of 2 Years Ago by Momerath and prvnkmr194

This question has already been solved: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page rendered in 0.0921 seconds using 2.71MB