Data lost from .mdf file as I exit application - c#

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

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..!

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.

Related

WPF Desktop app how to include a sqlite DB

I am developing a WPF desktop application that uses a SQLite DB and this is in my App_data folder which is fine while a run it in debug but how do i include a DB file when i build my application and run it on another computer? do i need to build an installer for my app to create a copy of the DB in a location on the users machine that has read/write access?
Thanks
You could create the database in your initialization routines if the database doesn't exist. You can include the location and other settings in your app config. Check this link out: Create SQLLite Database and Table
It sounds like you want to have your SQLite database included in your project output including all the records that were added (i.e. you do NOT want to deploy an empty copy of the database, but rather the same data that you were working with during development).
To accomplish this, add the SQLite database to your project and set the build action to "copy to output location". In your config file, you can then set up the connection string to look for the file in the application directory.
Now, every time you build the project, a fresh copy of the database will be placed in the output directory.

Unable to update database .MDF is Read Only

I have a local DB that I have in an application. When I install it on another machine I get the error
Unable to update database.. .mdf is read only.
I was able to get around it by editing my permissions on the .mdf and log file themselves. I did some research and noticed that I may want to install the database to a shared folder. However, I am not sure how to do that and the answers I did come across did not make a whole lot of sense to be.
//My connection string
SqlConnection con = new SqlConnection(#"Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|AssetDatabase.mdf;Integrated Security=True");
Any guidance would be greatly appreciated. I am learning.
Just go to the program files and find the folder of the installed program.
Just right click the .mdf file in the folder and click PROPERTIES.
In PROPERTIES TAB -> SECURITY -> you'll see Group or Usernames.
In that select the User to which u want to give Access to the file.
If for that user the PERMISSION is not set to FULL CONTROL.
TO CHANGE PERMISSIONS -> CLICK EDIT.
Now the Group or Username box will open.
In that Select The User -> Select FULL CONTROL in Permission Box.
Now follow the steps for the log file too. IF Needed.
OR
Just install your application in different folder other than ProgramFile(x86)
The MDF file would be readonly either because the readonly file attribute flag is set, in which case you have to unset it, or another program has the MDF file locked as readonly. Are you running a version of SQL server that is using that file?
I've never seen a connection to the MDF file directly, usually it's done via the server, and the server will manage all the IO for the MDF file.
E.g. a typical connection string for SQL Server:
Database=<dbname>;Server=<servername>;MultipleActiveResultSets=True;Connection Timeout=10;User Id=<username>;Password=<password>;
Additional links:
1) Failed to update .mdf database because the database is read-only (Windows application)
2) Failed to update database because it is read-only
3) Failed to update database "*.mdf" because read only EntityFramework
4) http://www.codeproject.com/Questions/183758/Failed-to-update-mdf-database-because-the-database
First stop the SQLEXPRESS service from local-Services menu and then try to move and connect the database again by attaching database option in SQLEXPRESS. it should work. it works for me. :)
I solved the same problem as follows:
While creating "Setup", I manually added my database files database.mdf and database_log.ldf into the Application Folder file. But after adding these files, make sure that the ReadOnly option in the Properties section is False.
Also "connectionString" I use:
connectionString="Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\database.mdf;Integrated Security=True;Connect Timeout=30"
just remove the .mdf file location to c:\ or desktop or anywhere but not in
%program file directory
if your database file on the C volume,
try to move the .mfd file to D volume

"Cannot attach the file" error when deleting a database from the App_Data folder with LocalDb in MVC?

I am using LocalDb and I just want to delete my database.
I was under the impression that due to the way that LocalDb works I would just be able to access the *.mdf and *.ldf files that the local DB instance has created in my App_Data folder (I am using MVC5 by the way).
And that next time I start the application and use it they would just create new files and I would have a new database. But when I delete the files I get the following message...
Cannot attach the file 'c:\users\exitos\documents\visual studio
2013\Projects\xxxxxxxxxxxxxxxxxxxxxx\xxxxxxxxxxxxxxxxxxxxxx\App_Data\aspnet-xxxxxxxxxxxxxxxxxxxxxx-20140118101721.mdf'
as database 'aspnet-xxxxxxxxxxxxxxxxxxxxxx-20140118101721'.
Why? I did a reset to my initial commit using GIT to make sure the entire solution folder was 'box fresh' and I still get the problem. I even stopped the Sql Server Express from running and killed all Sql processes and still the same problem.
Is the .ldf and .mdf file written to the registry or something?
I changed the name of the file in the .config and it worked but I don't want to have to keep doing that. I just want to tear down my database by physically deleting it...
This is not obvious but it is advised that you should not delete the physical .mdf and .log files outside of the SQL Server Object Explorer in Visual Studio or in SQL Server Management Studio.
If this happens you can un-register it by using the object explorer.
Go to Server Explorer > data connections, find the db and delete it from there.
Visual studio will then re-create the db next time without an issue.
I learnt about this from a video by Scott Allen but he's also documented it here.
http://odetocode.com/blogs/scott/archive/2012/08/15/a-troubleshooting-guide-for-entity-framework-connections-amp-migrations.aspx

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

Linq To Sql update mdf file using clickonce

I have a application that use Linq2sql and have a database (mdf file). I using clickonce to release the application. I need to make changes in the database structure, How can i do this and update the mdf files using clickonce without losing the data at the files?
If you place your mdf in the data directory then you can have access to that previous-version-mdf-file when the newly installed version is run for the first time, since old data files are accessible in a PRE directory.
So at that moment you can copy the data from the old mdf to the new mdf.
To give you the full picture, are data files are renewed when a new installation is done. Its up to you to copy any old data.
Link which explain the first run of a clickonce installation:
How do I detect the first time a ClickOnce-deployed application has been run?
Link which mentions the PRE data directory to access files from the previous version.
clickonce - does writing/reading to the Data Directory required Admin rights?

Categories

Resources