Click here to Skip to main content
15,893,668 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
This error has stuck me for many days. Please solve end my this headache.

I have Database1.mdf into solution explorer. I want to open this database locally into my application. I used below connection strings but any of them is not working.

Dim cons As String
     'cons = "Server = .\ SQLExpress;AttachDbFilename=" & Application.StartupPath & "\Database1.mdf;Database=Database1;Trusted_Connection = Yes;"
     'cons = "Server=(localdb)\v11.0;Integrated Security=true;"
     cons = "Server = .\ SQLExpress;AttachDbFilename=|DataDirectory|Database1.mdf;Database=Database1;Trusted_Connection = Yes;"
     'cons = "Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\Database1.mdf;Integrated Security=True;Connect Timeout=30"
     'cons = "Server=(localdb)\v11.0;Integrated Security=true;AttachDbFileName=" & Application.StartupPath & "\Database1.mdf;"
     DbLib.GridViewQuery(Me.dgv, "Select * from Table1", cons)


It is giving me this error.

Additional information: Directory lookup for the file "C:\Users\Rizwan Chattha\Documents\Visual Studio 2015\Projects\TestDb\TestDb\bin\Debug\Database1.mdf" failed with the operating system error 5(Access is denied.).

Cannot attach the file 'C:\Users\Rizwan Chattha\Documents\Visual Studio 2015\Projects\TestDb\TestDb\bin\Debug\Database1.mdf' as database 'Database1'.


Please guide me how to solve this problem.
Posted
Comments
Mathi Mani 6-Jan-16 17:09pm    
Did you try placing Database1.mdf in App_Data folder? If not place it under that folder and try to access.

1 solution

Most likely the account that is used to run the SQL Server service does not have permission to the folder where the mdf is located (...\bin\Debug\).

One option is to check and modify the folder security settings or to change the location of the database file to such folder where the SQL account has permissions. I believe the latter would be a good choice to start with.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900