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.

Recommended Answers

All 3 Replies

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

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.

Thank you. SqlConnectinStringBuilder will probably work.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.