Link to home
Start Free TrialLog in
Avatar of ArunVashist
ArunVashistFlag for India

asked on

Visual C#, Changing Connection string at runtime,

Hi Friends,

I am developing a windows application in VS 2008 using C# with MS-Access as database, this application will be installed on network and all instances of my application will use a database located at server through a shared drive which will be mapped at local PC,

Now I want to know how can i make my application to ask for the location of database at server and stored this location in connection string property (in settings.settings file). I want my application to ask this at very first run and then store the location somehow and use it till next change.

thanks in advance

SOLUTION
Avatar of jinal
jinal
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of ArunVashist

ASKER

Hi Jinal,

this sounds good, but I have many other alternative of storing information like in .ini file in .xml file etc., but what I really want to know is how to use stored lnformation in Connection string. e.g. while development I create a connection string and its in settings.settings file, all my classes and datasets uses this connection string, so question is how to replace this connection string with stored location.

thanks,
Use a config file. If its a web application, there will be web.config file or if it's a windows application, there will be an app.config file (If not, you can always add them).
You can create your connection string, and store it in your config file. You can change it anytime you want.

When your application starts, gather all the information you need from the user (database name, server name, user id, password). Create a connection string and store it in your config file. You can also encrypt it before storing it.

HTH.
Hi Imperdonato,

Your suggestion seems pretty practical to me, can you send me a example code to use app.config  file, moreover can we store encrypted data in app.config. Well addition to this I also want to know :

1.  If we use app.config file, do I need to make changes in Dataset connections and Classes which are using connection string from settings.settings file.
 
           A. Following is the code which is use in classes to open dataconnection
                 conn_string = PatientCare.Properties.Settings.Default.mainConnectionString.ToString();

           B. same string is used in Typed Datasets and Crystal report database source.

please let me know what type of changes I have to do in order to make everything works flawless.

thanks,
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
And yes, you will need to change the way you are reading the value in your underlying classes.
Depending upon whether you decide to create a new section to store your connection string, or use the AppSettings section itself, the values will change.

HTH!
well Imperdonato,

Its really confusing here with me, my application already contains a app.config file but I am not able to access it the way suggested, even I am not able to store/update connection string in it.

any change made in app.config reflects in settings.settings and vice versa.

please guide.
Thanksk