SQL Server Developer Center > SQL Server Forums > SQL Server Security > Access denied in attaching sql server database
Ask a questionAsk a question
 

AnswerAccess denied in attaching sql server database

  • Monday, November 02, 2009 7:06 PMfatih ozturk Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Hello

      I have customers that use my .NET program in their External Hard Drive or Flash Memories. I use SqlClient classes, sql server 2005 database and the database is located in the same directory with my program files in external unit. I attach the sql database to SQLEXPRESS catalog when application login form is shown and detach when the application closes.

       Attaching sql database is successful sometimes, but sometimes windows is prevent the access .mdf or .ldf file and access is denied exception is thrown although all users have administrator priviliges.

       In fact, solved the problem by using subinacl file inconveniently.

       My question is why the windows is denying access to mdf and ldf files by reason of they have no permission while windows do nothing other files in the same folder that has no permission? For example my main exe file runs with no problems.

       Thanks.

Answers

  • Wednesday, November 04, 2009 9:52 AMAjmer Dhariwal Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    It doesn't matter what the users privileges are. 
    When carrying out any file access within SQL Server it's the SQL Server service account that needs the access rights to any drives/folders that you wish to attach databases from. 
    If SQL Server is running a generic account like Network Service then it probably won't have the correct rights, so double-check the account SQL Server is running under and make sure it has sufficient privileges.


    ajmer dhariwal || eraofdata.com

All Replies

  • Monday, November 02, 2009 9:59 PMLekss Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    What was the operating

    system errors in all your cases ?

    Were the users copying the

    file to local disk and attaching or straight from ext drive ?


    Thanks, Leks
  • Tuesday, November 03, 2009 8:55 AMfatih ozturk Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Thanks for your reply Leks,

        Users run the program straight from external drive. They do not copy any file to local drives.

        Error was 5 (access is denied) as far as i remember and UnauthorizedAccessException is thrown in .NET when trying the SqlConnection to open. It is not important what the error is. Windows has so many security error to block you.

        As i said, achieved all problems that i encountered using sseutil, subinacl and writing .NET code during attaching db. In addition, users may plug ext drives into different computers and use the program on it.

        What i cannot understand is :

        1. Is computer user administrator? Yes.
        2. Does database engine accept AttachDBFileName parameter in connection strings? Yes... It is not obsolete.
        3. Does computer administrator access and open other files (includes executables) in the same location with no problem? Yes, he does.

        So, Why are the mdf/ldf files inaccessible?

    Thanks again.
  • Wednesday, November 04, 2009 9:52 AMAjmer Dhariwal Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    It doesn't matter what the users privileges are. 
    When carrying out any file access within SQL Server it's the SQL Server service account that needs the access rights to any drives/folders that you wish to attach databases from. 
    If SQL Server is running a generic account like Network Service then it probably won't have the correct rights, so double-check the account SQL Server is running under and make sure it has sufficient privileges.


    ajmer dhariwal || eraofdata.com