1
SqlConnection con = new SqlConnection(@"Server=.\SQLEXPRESS;AttachDbFilename='C:\HashTags.mdf';Integrated Security=True;User Instance=True");

con.Open();
String queryStr = "SELECT name FROM ttable WHERE name LIKE '*%'";
SqlCommand com = new SqlCommand(queryStr, con);
SqlDataReader sdr = com.ExecuteReader();

while (sdr.Read())
{
    this.trendingBx.Text = sdr.GetValue(0).ToString();
}

sdr.Close();

Could anyone tell me why I get this error:

A database with the same name exists, or specified file cannot be opened, or it is located on UNC share.

3 Answers 3

1

Are you sure the database is not already attached? If it is you should use:

Data Source=.\SQLEXPRESS;Database=your database name;Integrated Security=SSPI
Sign up to request clarification or add additional context in comments.

1 Comment

SqlConnection con = new SqlConnection(@"Server=.\SQLEXPRESS;Database=HashTags;Integrated Security=sspi");
0

You should use:

Integrated Security=SSPI

not

Integrated Security=True

in your connection string.

Comments

0

Assuming that the "specified file cannot be opened, or it is located on UNC share" you have already checked & the .mdf does exist at the location, I think you need not have the quotes in the connection string:

AttachDbFilename='C:\HashTags.mdf'

should be:

AttachDbFilename=C:\HashTags.mdf (no single quotes)

and this should work IMO.

Comments

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.