Failed to update .mdf database because the database is read-only (Windows application)

15,277

Solution 1

If the MDB file is in your application path, then the default permissions would require elevation of rights to be able to write to the files -- I'd recommend moving the data to the ApplicationData shared folder, where end users will have write permissions by default

Solution 2

The big thing that changed between Windows XP and Windows Vista/7 is the introduction of UAC which means that users, even if created as administrators, don't routinely have read/write access to "important" locations such as the %programfiles% (Usually C:\Program Files or C:\Program Files (x86)) directory. This is why your application works on Windows XP and not on Windows Vista.

You need to store your DATA in the %programdata% directory (which is usually C:\ProgramData) if the data is shared between users on the machine or %appdata% (which is usually C:\Users\USERNAME_GOES_HERE\AppData\Roaming) if it is specific to a given user. You'll now no longer encounter the problem of being unable to write to the file.

The reason for this is that by storing data in your programs installation directory you were doing the wrong thing. Windows didn't stop you from doing this previously, but it was fairly widely documented that %programfiles% was not the appropriate place to store data.

Share:
15,277
Ambarish
Author by

Ambarish

Updated on June 07, 2022

Comments

  • Ambarish
    Ambarish almost 2 years

    I've created a database windows application using in C#. My application is running successfully on Windows XP, but it doesn't properly execute on Vista or Windows 7 systems. My application shows a message similar to

    Failed to update .mdf database because the database is read-only

    Can anyone give me a solution to this?

  • Ambarish
    Ambarish about 13 years
    Thanks, Here i'm using this code,do you know Identity parameter of FileSystemAccessRule Class Directory.GetAccessControl(path); dirSec.AddAccessRule(new FileSystemAccessRule("?", FileSystemRights.Read, AccessControlType.Allow)); Directory.SetAccessControl(path, dirSec)
  • Vilius Surblys
    Vilius Surblys about 13 years
    @Ambarish you really should consider saving the data file in a path that is intended for data, which would make that step unnecessary - you can after all you can use Environment.GetFolderPath(Environment.SpecialFolder.Applicat‌​ionData)) to retrieve the location programmatically for per-user settings (or change to use CommonApplicationData for per-machine data)
  • rahularyansharma
    rahularyansharma about 12 years
    @Shaw how can i store my database my files to C:\ProgramData
  • Mahmood Jenami
    Mahmood Jenami over 8 years
    Yes dear @Ambarish It dose work; But why you didn't marked it as answer?