0

My C# desktop application when run from Windows Guest Account gives this error while accessing SQLLocalDB: "failed to update database "C:\ProgramData\myDB.mdf" because the database is read-only." (I placed the DB in ProgramData to make it accessible to all users)

When run from Windows Administrator Account there is no problem.

The Application is installed on the computer through a SETUP Installer project (NOT CLICKONCE, because Clickonce apps are only accessible on the Windows User account that installed it. I want this app to be accessible to all Windows users on the same machine).

The Installer also installs SQL Server 2016 Express LocalDB.

The application uses a single Database for all Windows users (on the same machine). This is a home business app, meant to be installed by end user without any professional help. If it is possible, how do I go about it?

EDIT: It seems to be a Windows permissions problem, because when Run as Administrator from the Guest Account, the app works. No more errors. But is there a way to set this automatically, perhaps during SETUP/install?

Vince
  • 3
  • 3
  • I somewhat doubt that the guest account will have access to modify files within the Program Files directory. – Thom A Feb 04 '21 at 13:24
  • sorry, i placed the correct path after ur comment – Vince Feb 04 '21 at 13:38
  • `This is a home business app, meant to be installed by end user without any professional help` Then SQL Server is a bad choice of DB, I would suggest to use SQLite or any other non-server DB engine. – Alejandro Feb 04 '21 at 13:46
  • Alejandro, interesting insight. Do u mean to say, that my choice of SQL Server LocalDB is problematic for this type of build? – Vince Feb 04 '21 at 14:44
  • See [this answer](https://stackoverflow.com/a/22506141/14868997) for how to share LocalDB (normally it runs per user only). Are you are using `AttachDbFileName` in the connection string? @Alejandro What is wrong with using LocalDB? Windows itself uses it under the hood – Charlieface Feb 04 '21 at 14:53
  • Charlieface, yes, I think. This is my connection string: $"Data Source=(LocalDB)\\CatMgrDBServer;AttachDbFilename={destFilePath};Integrated Security=True", where {destFilePath} is path to C:\ProgramData\myDB.mdf – Vince Feb 04 '21 at 15:08
  • @vincegrapes If you want to have a simplistic deploy, able to be done by non-technical user, normally SQL Server (including LocalDB) could be problematic. It's a full fledged server that requires a somewhat complex installation and maintenance procedure, being LocalDB geared toward development instead, and having a non-trivial setup for sharing among user accounts. Compare that, for example, to SQLite and similar libraries, which you simply copy a single .DLL file and the whole thing is just working. Trivial copy-paste deployment. – Alejandro Feb 04 '21 at 15:14
  • @Charlieface Nothing wrong per-se with it, but the fact that the OP wants a trivial deployment procedure geared towards non-technical people seems to suggest against it. Windows uses it under the hood? It's not included in any way in Windows, much less used by Windows itself for anything. If it were, using it would be trivial :P – Alejandro Feb 04 '21 at 15:18
  • @Alejandro LocalDB is the wrong word here, it's a similar private edition with it's own deployment. See [What is Windows Internal Database Edition?](https://blog.sqlauthority.com/2015/09/30/sql-server-what-is-windows-internal-database-edition/) – Charlieface Feb 04 '21 at 15:21
  • @vincegrapes this is going to cause issues. See [Bad habits: Using AttachDBFileName](https://www.sentryone.com/blog/aaronbertrand/bad-habits-attachdbfilename) – Charlieface Feb 04 '21 at 15:21
  • Alejandro, in the eventuality where I might have to switch to SQLite, do I have to rewrite all my Sql Command strings (class SqlCommand)? – Vince Feb 04 '21 at 15:31
  • You would have to rewrite it, yes. But I don't see why you would bother, LocalDB is nice and lightweight for your purposes. But you need to fix `AttachDBFileName`, and you need to follow instructions here: https://stackoverflow.com/a/22506141/14868997 for how to share LocalDB between users. – Charlieface Feb 04 '21 at 19:58
  • Charlieface, yes I took a look at the links u provided, but I am still confused. For example, the article does not give an example string without using AttachDBfilename, and the share localDB does not show how to do this in code. Would you mind posting a more detailed answer? – Vince Feb 05 '21 at 01:22

0 Answers0