Error 5 when trying to connect to database

  • I am trying to connect to SQL Server 2008 R2 Express from Visual Studio 2012. When executing the VS code it returns the following error:

    [SqlException (0x80131904): Directory lookup for the file "c:\users\austens samsung\documents\visual studio 2012\Projects\learning1\learning1\App_Data\test.mdf" failed with the operating system error 5(Access is denied.).

    CREATE DATABASE failed. Some file names listed could not be created. Check related errors.]

    Do I need to be running VS as administrator? My server is up and running and connected?

    The code is trying to create the database test.

    Any help would be appreciated.

  • This error usually happens when the account the SQL Server is running under does not have permission to the drive/directory/file being mentioned. Check the account's permissions and make sure the path is relative to the server, not your client.


    And then again, I might be wrong ...
    David Webb

  • Not sure how to go about doing that. Can someone walk me thru it? Thanks

  • easiest fix is to move it out of your personal folders:

    c:\users\austens samsung

    a fresh installation of SQL has permissions to it's own folders under program files, and it's own folders under program files(x86), but typically does NOT have permissions to specific users folders.

    my 2008 isntallation, for example, typically puts files here by default:

    C:\Program Files\Microsoft SQL Server\MSSQL10.MSSQLSERVER\MSSQL\DATA

    Lowell


    --help us help you! If you post a question, make sure you include a CREATE TABLE... statement and INSERT INTO... statement into that table to give the volunteers here representative data. with your description of the problem, we can provide a tested, verifiable solution to your question! asking the question the right way gets you a tested answer the fastest way possible!

  • Im going to post my connection string from my VS project. Can you tell me what the path should look like?

    <connectionStrings>

    <add name="DefaultConnection" providerName="System.Data.SqlClient" connectionString="AUSTENSSAMSUNG\SQLEXPRESS;Initial Catalog=aspnet-learning1-20130420131735;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\aspnet-learning1-20130420131735.mdf" />

    <add name="test"

    connectionString="Data Source=AUSTENSSAMSUNG\SQLEXPRESS;AttachDbFilename=|DataDirectory|\wingtiptoys.mdf;Integrated Security=True"

    providerName="System.Data.SqlClient " />

    </connectionStrings>

  • So let me understand. Where do I install my SQL Server during the installation? Or is it a problem with the VS project? Confused.

  • austen.robinson (4/24/2013)


    So let me understand. Where do I install my SQL Server during the installation? Or is it a problem with the VS project? Confused.

    Ok your problem is purely permissions to a specific folder.

    The SQL Service typically runs as a user who is not a local admin on the operating system, so cannot read data from any users Desktop/documents:

    your fix is simple: if you are going to attach a database via conneciton string, the file cannot be in your personal folders.

    Create a c:\SQLData folder, for exmaple, and put it there, or put it in the folder i mentioned above, since that is where SLq has at least four other databases already.

    then change your connection string to match the new folder:

    <connectionStrings>

    <add name="DefaultConnection" providerName="System.Data.SqlClient" connectionString="AUSTENSSAMSUNG\SQLEXPRESS;Initial Catalog=aspnet-learning1-20130420131735;Integrated Security=SSPI;AttachDBFilename=C:\SQLData\aspnet-learning1-20130420131735.mdf" />

    <add name="test"

    connectionString="Data Source=AUSTENSSAMSUNG\SQLEXPRESS;AttachDbFilename=|DataDirectory|\wingtiptoys.mdf;Integrated Security=True"

    providerName="System.Data.SqlClient " />

    </connectionStrings>

    the second option is to go to the service, and make it start as yourself...since you are a local admin on your own laptop/machine, the permissions problem would also go away:

    you can prove this is the issue by simply putting in your credentials, with your domain account and password, and confirm the external object you were trying to access/use works when SQL is run your credentials, so you'd know you need a domain or local account to access the resource.

    note you have to stop and start the service after you change the credentials before it will start using those credentials.

    Lowell


    --help us help you! If you post a question, make sure you include a CREATE TABLE... statement and INSERT INTO... statement into that table to give the volunteers here representative data. with your description of the problem, we can provide a tested, verifiable solution to your question! asking the question the right way gets you a tested answer the fastest way possible!

  • I like the first option you gave. What is the entire path in the "This User" above in the example you provided?

  • Ok, here is my current connection string. I now throws the error:

    Invalid value for key attachdbfilename

    Connection string: (this is where the rest of my databases are located)

    <connectionStrings>

    <add name="DefaultConnection" providerName="System.Data.SqlClient" connectionString="C:\Program Files\Microsoft SQL Server\MSSQL10_50.SQLEXPRESS\MSSQL\DATA;Initial Catalog=aspnet-learning1-20130420131735;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\aspnet-learning1-20130420131735.mdf" />

    <add name="WingtipToys"

    connectionString="Data Source=C:\Program Files\Microsoft SQL Server\MSSQL10_50.SQLEXPRESS\MSSQL\DATA;AttachDbFilename=|DataDirectory|\wingtiptoys.mdf;Integrated Security=True"

    providerName="System.Data.SqlClient " />

    Havent moved my VS project yet.

    If I created the folder you suggested (c:\SQLData) and put my project there will it clear the attachdbfilename error?

  • I'm going to post my Visual Studio project and if anyone can get the connection string to work and tell me how they did it I would appreciate it.

Viewing 10 posts - 1 through 9 (of 9 total)

You must be logged in to reply to this topic. Login to reply