1

I am building a .NET 4.0 C# application which connects to a web service, pulls some data, then inserts it into a database. This application is being designed to require no user input as it will be scheduled to run daily.

When I deploy this application to our customers, I run into a problem. Some of them are using SQL Server for their back-end and some are using MS Access. The only solution that I can come up with is to pass the connection string as a command line parameter, but I don't prefer that method. Since I have to deploy this to over 70 different customers, I would rather not compile a copy of the program for each customer.

Any thoughts and ideas are appreciated.

4
  • 3
    Put it in the app.config file. Commented Sep 16, 2013 at 13:29
  • Is it a windows service? Commented Sep 16, 2013 at 13:30
  • Put the connection string in the app.config file Commented Sep 16, 2013 at 13:30
  • I thought I had read that using app.config was an outdated way of doing things. Commented Sep 16, 2013 at 13:32

1 Answer 1

3

You can have the connection string in the App.config file. During the application deployment you can check the user machine whether SQL Server is installed by querying registery. based on the this result update app config with database specific connection string.

App.config

<?xml version="1.0" encoding="utf-8"?>
<configuration>

<appSettings>
<add key="DbConnectionString" value="Will be updated during deployment" />
</appSettings>

</configuration>

Updating connection string in app.config:

Configuration configuration = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
configuration.AppSettings.Settings["DbConnectionString"].Value = "DB Specific Connection string";

//Save only the modified section of the config
configuration.Save(ConfigurationSaveMode.Modified);

//Refresh the appSettings section to reflect updated configurations
ConfigurationManager.RefreshSection(Constants.AppSettingsNode);
Sign up to request clarification or add additional context in comments.

5 Comments

Won't I have to hard-code the connection string into my application then? Where would I be getting the connection string from?
@DoubleJ92 you dont have to hardcode it. Read it from App config file. See the edited answer.
I must be missing something then. The initial value of DbConnectionString is "Will be updated during deployment" When the program runs, that value gets updated to the actual connection string. Where does that come from?
@DoubleJ92 Yes, It will be empty in when you deploy. During the deployment based on the DB type you should update the connection string. thereafter your application will read the DB specific connection string.
@DoubleJ92 Yes, DB specific connection string should be kept as constant or in resource file. I'm doing the same for my project and it works fine.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.