2

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

user3004110
  • 929
  • 10
  • 24
  • 36

3 Answers3

4

Try ALTER DATABASE MyDatabaseName SET READ_WRITE

More informations here on This forum

Edit This was asked by someone else

Community
  • 1
  • 1
Jiyan Akgül
  • 158
  • 10
  • Thanks for the reply. My problem is different I have a read write problem on folder where the application installed. Ur solution works after the problem but what I need is to overcome this issue before it occurred. I mean whenever application installs it already have read write permissions on that particular folder where application installed, so that I will never face this issue, thanks. – user3004110 May 24 '14 at 16:26
  • I have a Sql server 2012 LocalDB. When i run alter query it runs for a long time and never give result. How to run alter in LocalDB ? – user3004110 May 24 '14 at 17:32
  • alter database [C:\PROJECT POS\PROJECTPOS\BIN\DEBUG\DATABASE\UNIVERSAL_DATA.MDF] set read_write...... When i run this query, it give this error now:- User does not have permission to alter database 'C:\PROJECT POS\PROJECTPOS\BIN\DEBUG\DATABASE\UNIVERSAL_DATA', the database does not exist, or the database is not in a state that allows access checks. – user3004110 May 24 '14 at 20:23
  • I can see this database in sql server management studio with dark gray (read only) image, thanks – user3004110 May 24 '14 at 20:29
  • Are you logged in as Admin? (Studio and App). And have you tried the solution of the link I added to my answer? – Jiyan Akgül May 25 '14 at 00:17
  • Thanks for reply. Iam not logging from any user its open for everyone but I Done it manualy by right clicking on the folder and give full control to everyone group. But my question is still there that during installation how can I programatically set read write permissions to folder so that the end user will never face the above screenshot error?. Thanks for your patience. – user3004110 May 25 '14 at 04:40
  • **[Look at the link I addet to my answer](http://stackoverflow.com/questions/17027788/localdb-readonly-database-problems)**. Here a sample `icacls mydabase*.* /grant "MYMACHINENAME\Administrator":(F)`. *[What is icacls?](http://technet.microsoft.com/de-de/library/cc753525(v=ws.10).aspx)*. This should help. – Jiyan Akgül May 25 '14 at 10:44
  • Thanks for reply and sorry for long previous comments... Now two questions arise: 1-Where to write this code? 2-How to implement at the moment when installation completes?, Thanks. – user3004110 May 25 '14 at 19:28
  • This is no code. You call it with an execute command. `Icacls.exe ...`. I don't know what you are using to install your software, but it should support calling an executable. – Jiyan Akgül May 25 '14 at 19:36
  • I try to execute command on sql server but it not works. Iam going to mark as answer and opening a new thread. I actually needed a VB or c# code to solve this issue. So i will take in a new thread. Anyways thanks for your each comments. – user3004110 May 28 '14 at 04:10
1

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.

Andrew Morton
  • 24,203
  • 9
  • 60
  • 84
  • Thanks for the reply, i have understanding about these special folders. But do you think that putting application database in these folders is best option other than application folder where an app installs? – user3004110 May 24 '14 at 20:26
  • @user3004110 Yes. It will make it work and follows Microsoft's guidelines for where to store data. The application folder should be read-only (for a general user) to hinder malware. – Andrew Morton May 24 '14 at 20:30
  • Thanks Andrew, iam near to conclusion but please tell me one thing more. When I install application the database files are also placed in application folder where I have another folder name database. Now my question is what should I do to move this database folder to special folder? Should I programatically move from application folder to special folder when an application runs first time? Or there is another better way? – user3004110 May 24 '14 at 20:41
  • @user3004110 Start here: [VS Setup project: install files in different directories](http://stackoverflow.com/questions/11499301/vs-setup-project-install-files-in-different-directories). Any further questions about it would probably warrant a new question if your searching Google etc. doesn't solve it. – Andrew Morton May 24 '14 at 20:45
0

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.