Changing connectionstring (preferably only database location) at runtime?
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.
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
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.