Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Error 5 when trying to connect to database


Error 5 when trying to connect to database

Author
Message
austen.robinson
austen.robinson
Grasshopper
Grasshopper (12 reputation)Grasshopper (12 reputation)Grasshopper (12 reputation)Grasshopper (12 reputation)Grasshopper (12 reputation)Grasshopper (12 reputation)Grasshopper (12 reputation)Grasshopper (12 reputation)

Group: General Forum Members
Points: 12 Visits: 23
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.
David Webb-CDS
David Webb-CDS
SSC Eights!
SSC Eights! (920 reputation)SSC Eights! (920 reputation)SSC Eights! (920 reputation)SSC Eights! (920 reputation)SSC Eights! (920 reputation)SSC Eights! (920 reputation)SSC Eights! (920 reputation)SSC Eights! (920 reputation)

Group: General Forum Members
Points: 920 Visits: 8584
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
austen.robinson
austen.robinson
Grasshopper
Grasshopper (12 reputation)Grasshopper (12 reputation)Grasshopper (12 reputation)Grasshopper (12 reputation)Grasshopper (12 reputation)Grasshopper (12 reputation)Grasshopper (12 reputation)Grasshopper (12 reputation)

Group: General Forum Members
Points: 12 Visits: 23
Not sure how to go about doing that. Can someone walk me thru it? Thanks
Lowell
Lowell
SSChampion
SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)

Group: General Forum Members
Points: 14958 Visits: 38972
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!

austen.robinson
austen.robinson
Grasshopper
Grasshopper (12 reputation)Grasshopper (12 reputation)Grasshopper (12 reputation)Grasshopper (12 reputation)Grasshopper (12 reputation)Grasshopper (12 reputation)Grasshopper (12 reputation)Grasshopper (12 reputation)

Group: General Forum Members
Points: 12 Visits: 23
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>


austen.robinson
austen.robinson
Grasshopper
Grasshopper (12 reputation)Grasshopper (12 reputation)Grasshopper (12 reputation)Grasshopper (12 reputation)Grasshopper (12 reputation)Grasshopper (12 reputation)Grasshopper (12 reputation)Grasshopper (12 reputation)

Group: General Forum Members
Points: 12 Visits: 23
So let me understand. Where do I install my SQL Server during the installation? Or is it a problem with the VS project? Confused.
Lowell
Lowell
SSChampion
SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)

Group: General Forum Members
Points: 14958 Visits: 38972
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!

austen.robinson
austen.robinson
Grasshopper
Grasshopper (12 reputation)Grasshopper (12 reputation)Grasshopper (12 reputation)Grasshopper (12 reputation)Grasshopper (12 reputation)Grasshopper (12 reputation)Grasshopper (12 reputation)Grasshopper (12 reputation)

Group: General Forum Members
Points: 12 Visits: 23
I like the first option you gave. What is the entire path in the "This User" above in the example you provided?
austen.robinson
austen.robinson
Grasshopper
Grasshopper (12 reputation)Grasshopper (12 reputation)Grasshopper (12 reputation)Grasshopper (12 reputation)Grasshopper (12 reputation)Grasshopper (12 reputation)Grasshopper (12 reputation)Grasshopper (12 reputation)

Group: General Forum Members
Points: 12 Visits: 23
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?
austen.robinson
austen.robinson
Grasshopper
Grasshopper (12 reputation)Grasshopper (12 reputation)Grasshopper (12 reputation)Grasshopper (12 reputation)Grasshopper (12 reputation)Grasshopper (12 reputation)Grasshopper (12 reputation)Grasshopper (12 reputation)

Group: General Forum Members
Points: 12 Visits: 23
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.
Attachments
learning1.zip (1 view, 9.00 MB)
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search