Reputation: 303
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.
Upvotes: 1
Views: 222
Reputation: 8902
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);
Upvotes: 3