none
Operating system error 5: "5(Access is denied.)". CREATE FILE encountered operating system error 5(Access is denied.)

    Question

  • Hi i got an error, for many days i suffering from how to connect with database.

    i m using sql server 2008 R2

    I have two database one is using Applications services called ASPNET.mdf and other is created Customer.mdf, i hosted my web application on local server at wwwroot directyly ASPNET.mdf can be accessible by my application, but while connecting to HealthCareDB.mdf i am getting error

    Unable to open the physical file "C:\Database\HealthCareDB.MDF". Operating system error 5: "5(Access is denied.)".
    An attempt to attach an auto-named database for file C:\Database\HealthCareDB.MDF failed. A database with the same name exists, or specified file cannot be opened, or it is located on UNC share.

    When i gave Network Access to this database then i got again an error

    Unable to open the physical file "C:\Database\HealthCareDB_log.ldf". Operating system error 5: "5(Access is denied.)".
    CREATE FILE encountered operating system error 5(Access is denied.) while attempting to open or create the physical file 'C:\Database\HealthCareDB_log.ldf'.
    Could not open new database 'C:\DATABASE\HEALTHCAREDB.MDF'. CREATE DATABASE is aborted.
    An attempt to attach an auto-named database for file C:\Database\HealthCareDB.mdf failed. A database with the same name exists, or specified file cannot be opened, or it is located on UNC share.
    File activation failure. The physical file name "C:\Database\HealthCareDB_log.ldf" may be incorrect.

    could you please help me to came out from this situation.

    My connection string is like this

     

    <add name="HealthCareDBEntities1" connectionString="metadata=res://*/Models.HealthCareDB.csdl|
    res://*/Models.HealthCareDB.ssdl|res://*/Models.HealthCareDB.msl;provider=System.Data.SqlClient;provider connection 
    string=&quot;Data Source=.\SQLExpress;AttachDbFilename=C:\Database\HealthCareDB.mdf;Integrated Security=True;User 
    Instance=True;MultipleActiveResultSets=True&quot;" providerName="System.Data.EntityClient"/>

     

    <add name="HealthCareDBEntities1" connectionString="metadata=res://*/Models.HealthCareDB.csdl|

    res://*/Models.HealthCareDB.ssdl|res://*/Models.HealthCareDB.msl;provider=System.Data.SqlClient;provider connection 

    string=&quot;Data Source=.\SQLExpress;AttachDbFilename=C:\Database\HealthCareDB.mdf;Integrated Security=True;User 

    Instance=True;MultipleActiveResultSets=True&quot;" providerName="System.Data.EntityClient"/>

     

     

    thank you very much

    Monday, November 29, 2010 7:41 PM

All replies

  • Reading the error is seems like you are trying to overwrite or create a DB that is already there...

    I have not used the property but, you have the AttachDbFilename specified in you connection string and after reviewing BOL, that specifies to attach the mdf to an instance.  In your case it sounds like you already have the DBs attached and running in Express.

    Check http://msdn.microsoft.com/en-us/library/bb264564(SQL.90).aspx

    "Fortunately, the SQL Server client code includes an option called AttachDBFilename, which eliminates the need to have a DBA attach the database files to a server before they can be used. When the AttachDBFilename keyword is included in a connection string, the specified file is attached to the SQL Server instance and the client is connected to the newly attached database. The argument to the AttachDBFilename option is the name of the file to attach. Here is an example."

    I recommend using a standard security connection string which can be found here

    http://www.connectionstrings.com/sql-server-2008

    Verify you can indeed connect to the DB thorugh SSMS (Express) or a simple winform as well. 


    Ted Krueger Blog on lessthandot.com @onpnt on twitter
    Please click the Mark as Answer button if a post solves your problem!
    Monday, November 29, 2010 8:03 PM
    Moderator