Best way to set strongly typed dataset connection string at runtime?

后端 未结 7 1801
孤街浪徒
孤街浪徒 2021-02-20 01:19

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

7条回答
  •  情深已故
    2021-02-20 02:12

    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.

提交回复
热议问题