Recently I work on SQL Server 2008 R2 database. I create database and attach .mdf file of same database in my application with some default data in it . Run application default data coming properly. Now I insert, update some data in my application and its works fine. But as I exit application and again run application lastly added and updated data get lost but default data coming proper as earlier. Please help. Why new
-
2are you commiting your changes to database before closing? – Ehsan Jul 03 '13 at 09:14
-
Just make sure you have one mdf file, It seems to me you are inserting and updating into other DB. :) – Rajeev Bera Jul 03 '13 at 09:32
-
@Ehsan, Yes i am saving changes after every database operation. Its works fine if i connect to SQL server R2 on my local machine(By changing connection string in App.Config). – Ankush Madankar Jul 03 '13 at 09:51
-
well i guess than we need to see your code. – Ehsan Jul 03 '13 at 10:04
-
I found answer to my que and add answer for same. – Ankush Madankar Jul 04 '13 at 05:43
2 Answers
As mention by @Henk , @Microtechie , I scan my project folder and found there are 3 copies of .mdf file are there, 1st in project folder where code project([ProjectFolder ]) resides 2nd in [ProjectFolder]/bin/debug folder and 3rd in [ProjectFolder]/bin/release folder and suddenly solution to my que trigger in my mind. Problem not in multiple .mdf files in project folder, as I every time ‘Clean’ and ‘Build’ my solution new copy of .mdf file from [ProjectFolder] get copied into [ProjectFolder]/bin/debug folder, result in override of last .mdf file in same folder. Hence every time I build and run application only default data coming and last added and updated data get lost. Thanku all for your replies and precise answer..!

- 3,689
- 4
- 40
- 74
-
1Placed mdf file in different location from the project bin folder. – Ankush Madankar Jun 13 '17 at 06:47
What kind of object do you load your .mdf file data into? It is likely that you need to save the changes in that object before closing the application.

- 719
- 9
- 18
-
I used Entity Framework (.edmx). And every database statement is under 'using' store clause means as using scope end database connection get close. e.g `using(Mystore _store = new MyStore()) { // Database operations _store.SaveChanges(); }` – Ankush Madankar Jul 03 '13 at 09:43