1

I have this line for the connection with my connection string

SqlConnection conn = new SqlConnection("Data Source=(LocalDB)MSSQLLocalDB;AttachDbFilename=""C: \Users\BDV\Documents\Visual Studio 2015\Projects\Interface_notes\Interface_notes\Database1.mdf"";Integrated Security=True;Connect Timeout=30");

But I get this error :

Class System.String
Represents Text as a series of Unicode characters
Syntax Error, ',' expected

I think the problem is with the double quotation marks, but I can't figure out what I can do instead. Can someone help me with this? Thanks Unrecognized escape sequence

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • [You need `SqlConnection(@"Data Source=...`](https://stackoverflow.com/questions/6134547/what-does-the-prefix-do-on-string-literals-in-c) (and you have a stray space after C:) – Alex K. Nov 19 '17 at 19:19

3 Answers3

2

There are two errors in your connection string. One has been already explained to you. This is C# and you need to prefix your string with the verbatim character to avoid parsing error when your string contains a backslash. Also you should remove the double quote around your path. They are not needed here.

The second error is the missing backslash between the (LocalDB) and the instance name.

So you write

 SqlConnection conn = new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;
 AttachDbFilename=C:\Users\BDV\Documents\Visual Studio 2015\Project\Interface_notes\
 Interface_notes\Database1.mdf;
 Integrated Security=True;Connect Timeout=30");
Steve
  • 213,761
  • 22
  • 232
  • 286
1

You can use verbatim string and remove double quote from inside :

Change it to

SqlConnection conn = new SqlConnection(@"Data Source=(LocalDB)MSSQLLocalDB;
     AttachDbFilename=C:\Users\BDV\Documents\Visual Studio 2015\Project\Interface_notes\
     Interface_notes\Database1.mdf;
     Integrated Security=True;Connect Timeout=30");
Akash KC
  • 16,057
  • 6
  • 39
  • 59
0

Should be like

SqlConnection conn = new SqlConnection(@"Data Source=
(LocalDB)MSSQLLocalDB;AttachDbFilename=C:\Users\BDV\Documents\Visual Studio 
2015\Projects\Interface_notes\Interface_notes\Database1.mdf;Integrated 
Security=True;Connect Timeout=30");
Matthew
  • 37
  • 8