Link to home
Start Free TrialLog in
Avatar of Stacie
StacieFlag for United States of America

asked on

unrecognized escape sentence - Connection string

I get an error  on the backslah that say unrecognized escape sentence   on  SVSQ02\macomb_test

con = new SqlConnection("Data Source=SVSQ02\macomb_test;Initial Catalog=DropDown;Integrated Security=True");
        con.Open();

Open in new window


not sure why....
ASKER CERTIFIED SOLUTION
Avatar of Shaun Kline
Shaun Kline
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
If you don't prefix a C# String with @ all backslashes are the start of an escape sequence for special characters like new line \n or tab \t or many others, but \m is none of them.

Two solutions:
1. The escape sequence for backslash is \\
con = new SqlConnection("Data Source=SVSQ02\\macomb_test;Initial Catalog=DropDown;Integrated Security=True");

2. Tell C# you don't want to use escape sequences in the string with the @ prefix:
con = new SqlConnection(@"Data Source=SVSQ02\macomb_test;Initial Catalog=DropDown;Integrated Security=True");

Bye, Olaf.

Edit: I'm almost sure you've already seen sample code using the @ prefix. It's often the better solution for things like paths, especially unc path starting with "\\\\server\\share" are worse readable than @"\\server\share".