Directory lookup for the file failed with the operating system error 5 RRS feed

  • Question

  • Hi

    I have a Windows desktop application that uses a SQLServer Express database.  The database files are downloaded from my website, followed by the application download (written in VBE).  The first procedure is to attach the database to the SQLServer instance from within the application. I have two test machines one runs Windows XP x84, the other Windows 7 x64.  I never have issues on attaching the database files in my test envirnoment but twice now other people downloading have got the following error

    System.Data.Sqlclient.sqlexception (ox 80131904) Directory lookup for the file C:\Documtents and Settings\Administrator\Desktop\Tem1.mdf failed with the operating system error 5 (failed to retrieve text for this error. Reason 1815) changed database context to 'master'.

    I'm not sure what the problem is and therefore how to correct the issue.  I attach the database in code using the create statement e.g.

    cmd7.CommandText = "USE Master CREATE DATABASE Tem1 ON PRIMARY (FILENAME = '" & DBFullPath & "') LOG ON (FILENAME = '" & DbLogPath & "') FOR ATTACH"

    Any help or suggestions would be very much appreciated.




    Friday, September 10, 2010 12:30 PM

All replies

  • Hello Chris,

    In SQL Server the error code 5 always means "Access denied". Eighter the use don't have the permission to attach the database or the SQL Server can't access the database file because of file securities.

    Olaf Helper ----------- * cogito ergo sum * errare humanum est * quote erat demonstrandum * Wenn ich denke, ist das ein Fehler und das beweise ich täglich
    Friday, September 10, 2010 12:51 PM
  • Hi Olaf Helper

    Thanks for your reply.

    The way I have deployed the database is by copying it and using Iexpress as the download tool in order that it can be downloaded from my website.  If they have administrative permssions on their machine I thought they should be able to attach and also to access the database files that they had downloaded.  In downloading the application they also downloaded the prerequisite of SSE 2008. 

    I'm not really sure how to proceed from here.  Any further suggestions would be much appreciated.





    • Edited by ChrisCB Friday, September 10, 2010 4:10 PM more relevant now
    Friday, September 10, 2010 1:06 PM
  • Hi ChrisCB,


    Since this file is downloaded from your web site, this issue might be caused by this file is blocked.


    In order for troubleshooting, I would like to recommend that you unblock this file and try again. Please follow the steps to unblock this file:

    1.       Right-click this file and choose “Properties”

    2.       Click “Unblock” button

    3.       Click “OK” button

    4.       Try again


    If anything is unclear, please let me know.

    Tom Li
    Tuesday, September 14, 2010 12:05 PM
  • Hi Tom

    Thanks for your post and sorry for the delay in replying.

    On my .mdf & .ldf files there is no "Unblock" button in the properties dialog box, so could you please just clarify.  The computer that has this problem is off site and I am not able to check the specific .mdf & .ldf files at the moment.



    Wednesday, September 15, 2010 3:11 PM
  • OS err 5 => Access is denied.

    If this issue is specific to Win 7 - Its possible that UAC might be coming into picture here.

    Either choose Run as Admin when launching the app. or

    Disable UAC.

    Can you ask the user to connect to SQL instance using SQLCMD or Mgt Studio?

    Wednesday, September 15, 2010 10:50 PM
  • Hi Vijay

    No, the two instances were on Windows XP,  I wouldn't want to consider disabling UAC.  I could use SQLCMD or SSMS, but what I would like to achieve is to attach from within the windows application.



    Wednesday, September 15, 2010 11:10 PM
  • Hello Chris,

    I want to share my solution to the exact same problem. I know it has been like really late. Almost 10 years.

    So, apparently SQL Server user had access to C:\ directory for me. Of course this is a permissions issue with the user.

    Tuesday, January 7, 2020 7:40 AM