Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Error 5 when trying to connect to database Expand / Collapse
Author
Message
Posted Wednesday, April 24, 2013 2:30 PM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Thursday, February 13, 2014 8:31 AM
Points: 8, Visits: 16
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.
Post #1446225
Posted Wednesday, April 24, 2013 2:34 PM


SSC Eights!

SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!

Group: General Forum Members
Last Login: Today @ 2:31 PM
Points: 893, Visits: 6,882
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
Post #1446228
Posted Wednesday, April 24, 2013 2:36 PM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Thursday, February 13, 2014 8:31 AM
Points: 8, Visits: 16
Not sure how to go about doing that. Can someone walk me thru it? Thanks
Post #1446230
Posted Wednesday, April 24, 2013 3:00 PM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 2:46 PM
Points: 12,744, Visits: 31,065
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

--There is no spoon, and there's no default ORDER BY in sql server either.
Actually, Common Sense is so rare, it should be considered a Superpower. --my son
Post #1446240
Posted Wednesday, April 24, 2013 3:11 PM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Thursday, February 13, 2014 8:31 AM
Points: 8, Visits: 16
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>

Post #1446243
Posted Wednesday, April 24, 2013 5:06 PM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Thursday, February 13, 2014 8:31 AM
Points: 8, Visits: 16
So let me understand. Where do I install my SQL Server during the installation? Or is it a problem with the VS project? Confused.
Post #1446259
Posted Thursday, April 25, 2013 5:14 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 2:46 PM
Points: 12,744, Visits: 31,065
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

--There is no spoon, and there's no default ORDER BY in sql server either.
Actually, Common Sense is so rare, it should be considered a Superpower. --my son
Post #1446418
Posted Thursday, April 25, 2013 10:00 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Thursday, February 13, 2014 8:31 AM
Points: 8, Visits: 16
I like the first option you gave. What is the entire path in the "This User" above in the example you provided?
Post #1446590
Posted Thursday, April 25, 2013 6:29 PM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Thursday, February 13, 2014 8:31 AM
Points: 8, Visits: 16
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?
Post #1446759
Posted Thursday, April 25, 2013 7:48 PM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Thursday, February 13, 2014 8:31 AM
Points: 8, Visits: 16
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.

  Post Attachments 
learning1.zip (0 views, 9.66 MB)
Post #1446771
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse