Solved

Visual C#, Changing Connection string at runtime,

Posted on 2009-07-11
8
1,426 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 

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
 
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

Setting up LaraDock for Laravel

Learn how to set up LaraDock in a Laravel project - LaraDock gives us an easy way to run a Laravel application using Docker in a single command.

Question has a verified solution.

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

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…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…

630 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