質問

I am creating a bookingsystem but i ran in to a problem whith my database connections when i change from specific path to relative path for datasource, When i have a speciffic datasource it can update the database and add new items. But when i change to relative path, at runtime everything works just fine and everything but when i close the program everything that i have changed or added have been disposed and when i open it again nothing have been saved. This is how the relative path looks like

SqlConnection SQLCN = new SqlConnection
(@"Data Source=.\SQLEXPRESS;
AttachDbFilename=|DataDirectory|\Databas\TestDatabase.mdf;
Integrated Security=True;User Instance=True");

Any idea why it does like this?

役に立ちましたか?

解決

That's because you override the database on each build.

Probably you have the Build Action set to Content and the Copy to Output Directory set to Copy If Newer.

From @steve's link, regarding the Copy if newer value:

We don’t recommend this option for .mdb or .mdf files. The database file can change even when no changes are made to the data. Simply opening a connection on a data file (for example, by expanding the Tables node in Server Explorer) can mark it as newer.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top