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

后端 未结 4 549
别那么骄傲
别那么骄傲 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条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-18 10:43

    I have met this problem before. I solve it in such a way.
    (1)in your app.config file, put a placeholder in the connection string. the connection string will contains the file path of the access db file. replace the path with a special string.

      
        
        
        
      
    

    (2)when your application start, use Directory.GetCurrentDirectory to get app path. before create a connection, replace the ##path## with the actual path on client computer.

    static void test()
    {
        string s = ConfigurationManager.ConnectionStrings["test"].ConnectionString;
        s.Replace("##path##", Directory.GetCurrentDirectory());
        OleDbConnection conn = new OleDbConnection(s);
    }
    

提交回复
热议问题