• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 909
  • Last Modified:

C# Change Connection String upon deployment


I have built an WPF application. The application connects to SQL Server to retrieve some data. I wish to deploy my application on a friend’s computer. I have the exe file along with the msi file. I have a connection string in my settings.cs file that connects to my database, however my friends database will be in a different location to mine. What I would like to know is the best way to change the connection string when my friend goes to install the program on his computer?

Thanks for any help,
2 Solutions
Since I don't use Windows installer for my software (I've found Inno to be exponentially better) I'm not entirely sure how to do this in the installer, but you can make the program detect on launch if the connection string has been set, and if not ask for it and store it.

The best way to do this would be to use the registry.

RegistryKey myKey = Registry.CurrentUser.CreateSubKey("Software\\MYPROGRAM\\");
            object result = myKey.GetValue("ConnectionString", null);
            string connectionStr = "";

            if (result == null)
                string conStr = Microsoft.VisualBasic.Interaction.InputBox("", "Enter connection string", "Default Text");
                myKey.SetValue("ConnectionString", conStr);
                connectionStr = conStr;
                connectionStr = myKey.GetValue("ConnectionString").ToString();

Open in new window

You can avoid having to create an entire new form to take the input by simply including the Visual Basic .NET library that Microsoft provides and using the InputBox class.

This code works by first attempting to read the value of "ConnectionString" from HKCU/Software/MYPROGRAM. If it can't read it, it will ask for the user to enter it, the nstore it in the registry. If it was read, it will simply store the value in connectionStr for you to use later.
Vikram Singh SainiSoftware Engineer cum AD DeveloperCommented:
I have a connection string in my settings.cs file that connects to my database,

** I had such requirement while developing software for my client. So what I did ?

First way:

1. Created a .xml file in some directory when software run with connection string details in it.

2. Read connection string from that file.

3. User was having option of modifying those details from software itself. That is you can present user with form to input new connection string.

Second way:

1. If it is meant for testing on your friend system then you can provide him configuration file with connection string.

2. And he can modify those values in connection string and your software can read those details from there.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

Tackle projects and never again get stuck behind a technical roadblock.
Join Now