18

I developed an application (100% local, no access to servers) using SQL Server Compact 3.5, and it works fine on my computer. However, when I deployed it on another computer, it shows this error:

Access to the database file is not allowed. [ File name = data\BDApepucCE.sdf ]

I deployed on a Windows XP computer. It shows this error whenever I try to write on the database, however, it works when I read.

I did a test on a Windows 7 computer, and it worked 100%, except when the file was accessed through the homegroup (that is, local network), on which it failed on both read/write.

Platform: Windows 7, Visual Studio 2010, and .NET 4 Client Profile

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
WoF_Angel
  • 2,569
  • 9
  • 35
  • 54

9 Answers9

20

Make sure you use |DataDirectory| in your connection string. Here is an example

connectionString="Data Source=|DataDirectory|MyDB.sdf"

I had the exact same problem ay you and I managed to fix it by adding the |DataDirectory| to my connection string.

Good luck.

Ian Boyd
  • 246,734
  • 253
  • 869
  • 1,219
Payman
  • 438
  • 4
  • 8
  • Just for clarity's sake, your answer worked for me, but with only |DataDirectory| preceding the file name, not including any asterisks. –  Aug 17 '12 at 22:26
16

This worked for me: http://solutionevangelist.com/community/discussion/20/access-to-the-database-file-is-not-allowed.-system.data.sqlserverce.sqlceexception/p1

It it usually a permissions issue. I give "Everyone" full permissions to the .sdf file in App_Data, either via the GUI, or using

icacls database.sdf /grant Everyone:F
Whiz
  • 189
  • 2
  • 6
9

I also had this same problem. My solution was in TFS/SourceControl at the time when I deployed it and the sdf file was checked-in (ie read-only) and this caused me the exact same error as above. When I checked out the sdf file and deployed again, everything was fine. Thought I'd share that in case other TFS users experience the same issue.

Ghlouw
  • 1,440
  • 16
  • 19
  • Good, but we need a code to automate the removing of "readonly" file attribute once the CAB project is built (maybe using Post Build) – FindOutIslamNow Dec 07 '17 at 06:38
4

Here are a two other suggestions:

  • Try to run your Visual Studio as administrator
  • Check if the 'readonly' attribute is set on the sdf file
Edwin de Koning
  • 14,209
  • 7
  • 56
  • 74
  • Is there a technique to get around the 'readonly' setting? I found this post on MSDN, but it points to a dead link: http://social.msdn.microsoft.com/Forums/en-US/sqlsecurity/thread/d72a020e-bd5e-4f1f-99e3-b77337560021 –  Jul 14 '11 at 21:09
0

Also you may want to check and see if your Application Pool is using Network Service (not Application Pool) and the Network Service Permissions are set to full control under your AppData folder. This was my problem awhile ago and I forgot how I did it, but this question reminded me of how I did it.

yardpenalty.com
  • 1,244
  • 2
  • 17
  • 32
0

You just need to make 'Server Side Includes" allowed under the IIS menu 'Web Services Extensions'

that fixed all my problems.

Barkov
  • 1
0

The following solution worked for me:

"Program Files" in Windows is protected, so normal users cannot write there, your applications will probably work only with an administrator user.

Change the data folder path for your database to somewhere where all users have permission to write.

olleh
  • 1,915
  • 18
  • 21
0

This would not be the typical cause, but I ran into this error message when my connection string had the incorrect path to the CE SDF file. Once I fixed that the error went away.

Bryan B
  • 720
  • 6
  • 16
-1

Just run your application as administrator. I solved my problem using this.

Shakir Ali
  • 55
  • 3
  • 5
  • 11
  • Thank you very much, this solves the error when I attemp to make a new SQL CE 4 database in LINQPad4. – sevenkul Feb 05 '17 at 17:58