user771886
user771886

Reputation: 21

Visual Studios 2010 C# sql database read only error

Im working on a visual C# project with a local sql database. When I install the .msi file and run the program and then try to update something in the database I get the error "Failed to update database C:\setup\DBNC.mdf because the database is read-only" Im really having a hard time figuring this one out, any suggestions??

Upvotes: 2

Views: 2774

Answers (3)

koolpitt
koolpitt

Reputation: 39

I hope this helps someone out there. I ran into a similar problem but resolved it by deploying my database files to the App Data directory and modifying my connection string to point there instead. This magically solved the problem.

Upvotes: 1

user1228
user1228

Reputation:

I'm gonna bust out my magic 8 ball for a minute. Let's see...

Is OP creating the database from a .sql script run on the local server?
Reply hazy, try again

Is OP including the .mdb in the install package?
You may rely on it

Okay. So the database is being copied to that specific directory by the user via the install package...

Is OP elevating to admin when running the install package?
Outlook good

Okay, so OP is creating the directory and copying the file there during install, all under the aegis of the admin account.

Is OP attaching this file to a local instance of Sql Server?
Signs point to yes

Is the database configured as read only in Sql Server?
Reply hazy, try again

Has OP run ALTER DATABASE DBNC SET READ_ONLY?
Don't count on it

Okay, then has OP updated the NTFS security on C:\setup\ and the mdb to give read/write access to the account that is running Sql Server (NETWORK SERVICE or some other account)?
My sources say no

Bingo!

You create that directory as admin and copy the file there. The account running Sql Server does not have rights to alter the file, so you're getting that error.

You shouldn't deploy databases as files. You should create a .sql file which creates the database and all tables/relationships/etc within it. That way you are always assured the database can be accessed by the Sql Server instance. Also, its easier to version a sql script file than a mdb.

Upvotes: 5

TabbyCool
TabbyCool

Reputation: 2839

Have you checked the properties of the .mdf file to make sure it's not read-only? Sounds like that may be the problem.

Upvotes: 0

Related Questions