2

I'd like to allow my users to switch between different databases on the login page at runtime.

I currently have the ConnectionString stored in my App Settings file and all the datasets refer to this setting.

I have tried modifying this setting at runtime, but this seems impossible.

What is the best way to do this?

3 Answers 3

1

Could you not just have a second connection string in your config file? What sort of data access code have you got setup?

Sign up to request clarification or add additional context in comments.

2 Comments

all my datasets have their connectionstring as MyConnectionString (stored in app.setting)
Is your problem that all your datasets are individually accessing the value from the config file? If it is then why not create a single piece of code responsible for returning the connection string to your datasets, then have that code read the appropriate connection string.
1

You can force the code to pick up the connection string from disk by calling ConfigurationManager.RefreshSection prior to fetching the connection string:

ConfigurationManager.RefreshSection("connectionStrings");
string connectionString = ConfigurationManager.ConnectionStrings["someConnection"].ConnectionString;

Comments

0

Is the database encrypted?

If it's not something that requires a large amount of privacy, you can store the connection string on a per-user basis.

For example:

System.Properties.Default.MyConnectionString = "Blah";
System.Properties.Default.Save();

or

string myConnectionString = System.Properties.Default.MyConnectionString;

EDIT: These can originally be set in VS by going to Project -> Properties -> Settings and make sure you put them in "User" Scope (Application Scope is read-only during runtime unless I'm mistaken).

EDIT2: Regarding comment below:

SqlConnection myConnection = new SqlConnection();
//Set from a normal String saved in user's settings
myConnection.ConnectionString = System.Properties.Default.MyConnectionString; 
myConnection.Open();

3 Comments

Connection Strings are restricted to Application Scope :-( Thanks though
Unless I'm mistaken you can use the 'String' type though.
the connectiostring in properties.default is readonly, how is possible to change it?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.