Theun Arbeider
Theun Arbeider

Reputation: 5419

Local database inserts not being stored

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?)

Upvotes: 2

Views: 3001

Answers (2)

Theun Arbeider
Theun Arbeider

Reputation: 5419

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)

Upvotes: 0

Steve
Steve

Reputation: 216293

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

Upvotes: 4

Related Questions