0

I am trying to connect my SQL Server database file with a C# application but it is throwing an error. I have already searched for many solutions but none of them are working.

An attempt to attach an auto-named database for file

C:\Users\John\documents\visual studio 2010\Projects\myDb.mdf A database with the same name exists, or specified file cannot be opened, or it is located on UNC share.

Here is my code for conn:

string DBPath = @"C:\\Users\\SayyedHashmatAli\\Desktop\\IqraLibrarySystem\\IqraLibrarySystem\\IqraLibrarySystemDatabase.mdf";
System.Data.SqlClient.SqlConnection sqlConnection1 = new System.Data.SqlClient.SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=" + DBPath + ";Integrated Security=True;Connect Timeout=30;User Instance=True");

Please help. Thanks

3
  • 1
    Its a connection string problem Commented May 7, 2016 at 20:28
  • can u please explain a bit more. thanks Commented May 7, 2016 at 20:29
  • Either escape backslashes in your strings like this: string DBPath = "C:\\Users\\... or use a literal like this: string DBPath = @"C:\Users\.... Do not do both. Commented May 7, 2016 at 21:50

1 Answer 1

1

\SQLEXPRESS

This is a SQL Server Express installation. The account it runs under must be able to access...

C:\Users\SayyedHashmatAli\Desktop

which is not possible - this is a user private folder. It is among the WORST POSSIBLE PLACES. It is not only in your private folder (and I doubt you forced SQL Server to run under your user account), it is ON YOUR DESKTOP - a place a database has no business being, ever.

Put it into a place that the server actually can read.

Sign up to request clarification or add additional context in comments.

3 Comments

i have tried this and now it is saying that The Data Source value in the connection string specifies an instance of SQL Server that is not installed. To resolve this issue, you can either choose to install the matching instance of SQL Server or modify the Data Source value in the connection string.
So your connection string was random rambling and you do not even have SQL Express installed? Start with the basics then.
Either you did not, or you changed the instance name. Simple like that.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.