Local database inserts not being stored

前提是你 提交于 2019-12-02 04:28:25

问题


This is probably a stupid mistake of me.. but I can't seem to understand it.

I've created a new, empty C# Windows Forms application.

I added a Database (Based on a dataset) and have the file stored in my solution explorer.

I've added a table Test with column Name.

I add a record using new SqlCeCommand("insert into Test values('Name')", new SqlCeConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString)).ExecuteNonQuery();

However, I've even tried retrieving the same data and it all works perfectly.

But when I stop my project, and rebuild it.. all data is gone again?

Is there a way to fix this, or will this fix itself when I'll start using this project for what it is intended (Without the 500 rebuilds a day?)


回答1:


Your database file is listed in the project with this property

 Copy to destination directory = Copy Always

If this is true then every time you restart the project a fresh (empty) copy of the database file is copied from the project directory to the output directory (BIN\DEBUG or BIN\RELEASE) overwriting the database file used in the previous run. You could avoid this changing the property to Copy Never or Copy if newer




回答2:


The answer given by steve keeps copying the database over the existing one, which results in removing all data.

I've managed to fix this by putting "Copy Always" on, then in the explorer move the database to a different location and add it to the project. This way the database will never be overwritten and can be used in the program!

(However, this will probably raise a issue if/when I publish the project to another computer)



来源:https://stackoverflow.com/questions/16069728/local-database-inserts-not-being-stored

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!