user3004110
user3004110

Reputation: 969

Failed to update database becase the database is read only

I have created a winform application and programmatically trying to attach the database when an application runs first time. Unfortunately in windows 7 i always got an error. Please view the screenshot below it tells the whole story. Now my question is that how can i get rid from this error, is there any way to automatically give required rights on the folder where the application installs?. I want to permanently resolve this error and need smooth attachment. Anyone please help.

Please view the error below. Thanks in advance

enter image description here

Upvotes: 2

Views: 17867

Answers (3)

Rashedkoutayni
Rashedkoutayni

Reputation: 51

I know this answer is somehow late, but I believe people always face the same problems so my case is worth to be shared.

tl;dr = Change the permission of deployed files manually or using icacls command.

Actually I use InstallForge for packing and deploying my application(s). No matter what setup creator is used, when the application is installed to a non-system folder ( e.g. D:\ ) the program works perfectly and the database is readable/writable.

Whereas when the application is installed in [Program Files] folder or [Program Files (X86)] folder, Windows takes a preventive security measurement and sets file permission to be only [Read] and [Read & Execute].

I think Windows Vista and later versions of Windows have this behavior.

You can check that by right-clicking the installed file and going to properties then Security tab.

The files I installed on D:\ had Full-Control permission while, as I mentioned, the ones on C:\ had only Read & Execute permissions.

You won't notice the difference when you install a normal program on C:\ because you might not be writing data on a file or a database. But in case of database deployment, the file has to be writable.

Finally, the solution for this case was telling InstallForge to change file permissions at the end of the installation using icacls commands :

icacls "C:\MyApp\MyDB.mdf" /Grant Everyone:F
icacls "C:\MyApp\MyDB_log.ldf" /Grant Everyone:F

In my case, it is okay to give everyone full-control on the database files, but you might need a customized solution for your case so please refer to : http://ss64.com/nt/icacls.html

You can tell your setup creator to run those commands, or you can put them together in a batch file and run it after the installation.

Upvotes: 0

Andrew Morton
Andrew Morton

Reputation: 25066

If you put your database in your own subdirectory of the directory returned by Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData then the user will have read/write access to it.

See Environment.SpecialFolder Enumeration to determine if a different location would be more suitable, e.g. ApplicationData if you need it to roam or CommonApplicationData if you need all users of the computer to use it.

Edit: I found a slightly more extensive version of this answer: Where Should I Store my Data and Configuration Files if I Target Multiple OS Versions?, please also see the articles it links to.

Upvotes: 1

Jiyan Akgül
Jiyan Akgül

Reputation: 160

Try ALTER DATABASE MyDatabaseName SET READ_WRITE

More informations here on This forum

Edit This was asked by someone else

Upvotes: 4

Related Questions