Change Connection String After Creating Setup File in C#.NET

后端 未结 4 553
别那么骄傲
别那么骄傲 2020-12-18 10:14

I am creating a C# windows form applications, the working can be summarized as users fills some form and data is saved in Access database. Now the problem I am facing is tha

4条回答
  •  梦毁少年i
    2020-12-18 10:45

    Suppose you deploy your app.config with this connectionstring

    "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\yourFile.accdb;"
    

    In a WinForms application the |DataDirectory| shortcut represent your application working folder, but you can change at runtime where it points to using this code.

    // appdomain setup information
    AppDomain currentDomain = AppDomain.CurrentDomain;
    //Create or update a value pair for the appdomain
    currentDomain.SetData("DataDirectory", "Your user choosen path");
    

    It eliminates the need to hard-code the full path which, has you have discovered, leads to several problems to resolve during install. Of course your setup should deliver your database in your user choosen path.

提交回复
热议问题