Elvis Mawira
Elvis Mawira

Reputation: 11

C# SqlConnection AttachDbFile does not recognize file path

I am new to C#, so forgive me for asking. I am trying to write a SqlConnection but the following code shows a red scribbly line where the db path begins. I have already copied the path from the database's connection string. It has given me lots of headache. Any help will be greatly appreciated.

SqlConnection con = new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename="D:\projects\Clinic POS\Clinic POS\Inventory.mdf";Integrated Security=True");

Upvotes: 0

Views: 89

Answers (2)

Bijay Budhathoki
Bijay Budhathoki

Reputation: 186

SqlConnection con = new SqlConnection("Data Source=(LocalDB)\\MSSQLLocalDB;AttachDbFilename=D:\\projects\\Clinic POS\\Clinic POS\\Inventory.mdf;Integrated Security=True");

Upvotes: 0

Andrey Nikolov
Andrey Nikolov

Reputation: 13450

You should remove the quotes around the file name:

SqlConnection con = new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=D:\projects\Clinic POS\Clinic POS\Inventory.mdf;Integrated Security=True");

Upvotes: 1

Related Questions