C# windows application access database data doesn't persist on close

前端 未结 1 492
孤城傲影
孤城傲影 2021-01-20 06:53

I\'m creating a windows application using C# whereby I\'m accessing an empty Access database which contains two tables: Provinces and Locations. I\'m working on the form th

相关标签:
1条回答
  • 2021-01-20 07:51

    This is a common scenario with file based database (or attached database files)
    Your connection string refers to the database without using any path.
    This means that your database is located in the same directory where your application runs.
    You don't have any problem inserting, modifying or deleting data but you loose everything when you restart the app from INSIDE a Visual Studio Debug Session.

    Now, if you look at your project files you probably have the database file listed between the other files. Between the properties of this database file you will notice the property Copy to the Output directory and its value set to Copy Always.

    This means that every time you restart your application from inside the Visual Studio environment that file is copied from the project folder to the output directory (usually BIN\DEBUG or BIN\x86\DEBUG) but this destroys the database used in the previous run removing the data inserted modified or deleted

    Change the property Copy to Output Directory to Copy Never or Copy if Newer

    However Copy If Newer presents another problem with MS-Access. If you open the database file located in your project directory using Access o using the Server Connection window of Visual Studio the file is immediately modified also if you don't change anything and thus the Copy If Newer will execute the copy to the output directory

    0 讨论(0)
提交回复
热议问题