Solved

Visual C#, Changing Connection string at runtime,

Posted on 2009-07-11
8
1,415 Views
Last Modified: 2013-12-17
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

0
Comment
Question by:ArunVashist
  • 4
  • 3
8 Comments
 
LVL 15

Assisted Solution

by:jinal
jinal earned 75 total points
ID: 24832804
If you Application is running in Local Drive in each system and each user has permission to read registry than you can store this information in registry.

These are the steps.

1. First of check for registry key and if it is not exists than application is running first time so prompt for servername using Form created by your application.

2. Also put menu option for setting connection string and this will use future change in connection string.

 RegistryKey key = Registry.CurrentUser.OpenSubKey(@"Software\MySoftware\Install",true);

            if (key == null)

            {

                // prompt for server name and store it over here.

                RegistryKey key1 = Registry.CurrentUser.CreateSubKey(@"Software\MySoftware\Install");

                key1.SetValue("DBServer", "testserver",RegistryValueKind.String);

            }

            else

            {

                // For later change

                key.SetValue("DBServer", "testserver", RegistryValueKind.String);

            }
 

            // To read

            RegistryKey key2 = Registry.CurrentUser.OpenSubKey(@"Software\MySoftware\Install", true);

            string servername = key.GetValue("DBServer").ToString();

Open in new window

0
 

Author Comment

by:ArunVashist
ID: 24832887
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,
0
 
LVL 5

Expert Comment

by:Imperdonato
ID: 24832889
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.
0
 

Author Comment

by:ArunVashist
ID: 24832974
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,
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 5

Accepted Solution

by:
Imperdonato earned 275 total points
ID: 24833195
Read the following on how to use app.config for you application:
1. http://www.experts-exchange.com/Programming/Languages/C_Sharp/Q_20932244.html
2. http://msdn.microsoft.com/en-us/library/ms254494.aspx

To encrypt/ decrypt, read: http://fredrik.nsquared2.com/viewpost.aspx?PostID=221 . You will need to implement something similar to that. Or use an hint provided from the first link I gave you.

You can also use: http://www.codeproject.com/KB/aspnet/webconfig.aspx for an example.
Please note you might need to change bit of code from the above links, but the overall picture should be the same.

HTH!
0
 
LVL 5

Expert Comment

by:Imperdonato
ID: 24833203
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!
0
 

Author Comment

by:ArunVashist
ID: 24835426
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.
0
 

Author Closing Comment

by:ArunVashist
ID: 31602483
Thanksk
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now