Connection string with relative path to the database file

后端 未结 10 739
暗喜
暗喜 2020-11-28 07:20

I load data from sdf database in winforms App. I use full path to the database file . Example :

conn = new SqlCeConnection

{

ConnectionString =\"Data Sou         


        
10条回答
  •  再見小時候
    2020-11-28 07:50

    In your config file give the relative path

    ConnectionString = "Data Source=|DataDirectory|\Database.sdf";
    

    Change the DataDirectory to your executable path

    string path = AppDomain.CurrentDomain.BaseDirectory;
    AppDomain.CurrentDomain.SetData("DataDirectory", path);
    

    If you are using EntityFramework, then you can set the DataDirectory path in your Context class

提交回复
热议问题