My Windows Forms application uses a strongly typed dataset created using the designer in Visual Studio. At runtime I would like to be able to select either the live or test data
Store connection strings for them both in an app.config and then you can switch based on a command line / start up switch. Or if you want to give the user the flexibility you could give them an options page where they can select which connection to use.
Below is the code to read a start-up switch:
string[] args = Environment.GetCommandLineArgs();
// The first (0 index) commandline argument is the exe path.
if (args.Length > 1)
{
if (Array.IndexOf(args, "/live") != -1)
{
// connection string =
// ConfigurationSettings.AppSettings["LiveConString"];
}
}
else
{
// connection string =
// ConfigurationSettings.AppSettings["TestConString"];
}
So now you start your app by calling:
MyApp.exe /live
Using MyApp.exe alone or with any other switch will get you the test configuration.