views:

700

answers:

3

Hi,

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

I've currently got the ConnectionString stored in my App Settings file and all the dataset's refer to this setting.

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

How is the best way to do this?

Thanks,

+1  A: 

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

Iain Hoult
all my datasets have their connectionstring as MyConnectionString (stored in app.setting)
Nathan Koop
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.
Iain Hoult
+1  A: 

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;
Fredrik Mörk
A: 

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();
McAden
Connection Strings are restricted to Application Scope :-( Thanks though
Nathan Koop
Unless I'm mistaken you can use the 'String' type though.
McAden