Solved

Visual C#, Changing Connection string at runtime,

Posted on 2009-07-11
8
1,418 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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 

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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

828 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