Solved

Visual C#, Changing Connection string at runtime,

Posted on 2009-07-11
8
1,416 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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Loop not working 29 47
asp.net mvc input file 2 32
I need help embedding an image as HTML in my vb.net application 3 48
C#.NET and microsoft certification. 3 31
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

911 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

16 Experts available now in Live!

Get 1:1 Help Now