3

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?

Rob
  • 45,296
  • 24
  • 122
  • 150
Ambarish
  • 60
  • 1
  • 1
  • 7
  • 2
    Where is the database file stored, how is it accessed? Are you using Sql Server user instances? **Show us some code**! =) – Rob Apr 19 '11 at 08:18
  • Database file is stored in my application path and i've using ado.net – Ambarish Apr 19 '11 at 08:30
  • **show us some code** - you've obviously got some, as it's not working and it'd certainly help us diagnose the issue! =) – Rob Apr 19 '11 at 09:37

5 Answers5

8

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.

Rob
  • 45,296
  • 24
  • 122
  • 150
  • Yes dear @Ambarish It dose work; But why you didn't marked it as answer? – Mahmood Jenami Dec 25 '15 at 08:14
  • Hi, I am following this: https://www.youtube.com/watch?v=nTIF8wNxcWY&list=PLHpWiazdjdBKA8HNGCbtiK7hzlW5h3eF-&index=14 . How can I change the connection string to AppData folder? I have setup project in the same for installing the aplication. – S. B. Aug 10 '22 at 14:03
  • @S.B. - ask a fresh question, *don't* use comments on someone else's question. You also need to *distil* what's relevant from the video. There is almost no chance that I (or anyone else!) will sit and watch a nine and a half minute long video to get context – Rob Aug 10 '22 at 14:18
2

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

Rowland Shaw
  • 37,700
  • 14
  • 97
  • 166
  • 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) – Ambarish Apr 19 '11 at 11:11
  • 2
    @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.ApplicationData))` to retrieve the location programmatically for per-user settings (or change to use `CommonApplicationData` for per-machine data) – Rowland Shaw Apr 19 '11 at 11:58
  • 1
    @Shaw how can i store my database my files to C:\ProgramData – rahularyansharma Jan 28 '12 at 15:10
0

I ran into this related to localdb, the file is named:

myfolder/mysolution/myproject/App_Data/something.mdf

The way I fixed it is to right-click on the top level folder (myfolder) and then choose Properties, then choose Edit, then select Users, add to users either the Modify permission or both Modify and full control (this is a development environment) and then click apply.

So in other words, in my experience, it doesn't matter what folder you put the localdb in, you just need to give Users permission to write.

Greg Pettit
  • 10,749
  • 5
  • 53
  • 72
0

You should add the Modify permissions for IIS_IUSRS user to *.mdf file.

andrey_t
  • 118
  • 1
  • 1
  • 6
-1

go to the folder Where the program is installed and right click on the database file and Properties -> Security -> Group or Username (Click users one by one and see below for the permissions)

If for the user if not set to full control, then click EDIT -> Select the user and give full control..

user1045265
  • 55
  • 1
  • 8