Database create error due to folder access right.

I have an application which will create a SQL database(using CREATE DATABASE) and the database files are supposed to be placed in c:\program files\myApp\data\myDatabase.mdf
However, when I try to do so, I get the following error:

System.ApplicationException: System.Data.SqlClient.SqlException: CREATE DATABASE failed. Some file names listed could not be created. Check related errors.CREATE FILE encountered operating system error 5(Access Denied) while attempting to open or create the physical file 'C:\Program Files\myApp\data\myDatabase.mdf'.

My window login already have administrator role.

The problem can be solved if I manually add full access right to my login in window exploder.  However, how can I do this programmatically   Or what is the proper way to create database located under my application folder


Answer this question

Database create error due to folder access right.

  • George S

    Are you referring to the SQL Server Express (SSE) installation or to your application installation
     
    In the first case, I don't think there is a way to grant access at installation time, unless the data folder is the same as your app data folder. For the second case, you can determine the SSE service account and grant it permissions to your folder at the time you install your application.

    Thanks
    Laurentiu



  • N2O

    As Mark pointed out, you will need to grant write access to that directory to the SQL Server service account. The error is not related to the permissions of the account that you use to connect to SQL Server when you execute the CREATE DATABASE statement.

    To find out the service account, execute Start->Run->services.msc and then look for the SQL Server service entry; you should find in it the name of the service account.

    Thanks
    Laurentiu

  • chilloutyo

    I have SQL Server Express and had that same problem.

    This directory works:
    C:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\Data

    Anything else like

    C:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\OtherFolder

    generated the above mentioned error. I don't remember granting any permissions at installation, so I suppose I have the default which only takes the ...\Data folder.

  • mackdoyle

    Anyone have a code sample of how to determine the SSE service account programmatically
  • Citrus

    Sorry that I am using SQL authenication and provide the sa password in the connection string.  How can I create database in my application folder


  • pavanr

    I see.  Thank you very much.
    If I would like to include SQL Server Express as a pre-requirite of my application, can I grant my app folder write access right of the SQL Server service account during installation
    Thanks again.

  • Peter Koechl

    The account that sql runs as needs to have the security privledge to access the directory or file.

    thanks,
    mark

  • Database create error due to folder access right.