Solved

Visual C#, Changing Connection string at runtime,

Posted on 2009-07-11
8
1,421 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Forrester Webinar: xMatters Delivers 261% ROI

Guest speaker Dean Davison, Forrester Principal Consultant, explains how a Fortune 500 communication company using xMatters found these results: Achieved a 261% ROI, Experienced $753,280 in net present value benefits over 3 years and Reduced MTTR by 91% for tier 1 incidents.

Question has a verified solution.

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

Suggested Solutions

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…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

752 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