Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unrecognized escape sequence in SqlConnection constructor

when ever I make connection it gives an error

Unrecognized escape sequence.

NEPOLEON\ADMN HERE ON THIS SLASH. NEPOLEON\ADMN IS MY SQL SERVER NAME.

SqlConnection con = new SqlConnection("Server=NEPOLEON\ADMN;Database=MASTER;Trusted_Connection=True");
like image 342
Pranav Mishra Avatar asked Sep 25 '12 15:09

Pranav Mishra


2 Answers

Escape the \ character like :

SqlConnection con = new SqlConnection("Server=NEPOLEON\\ADMN;Database=MASTER;Trusted_Connection=True");

or

SqlConnection con = new SqlConnection(@"Server=NEPOLEON\ADMN;Database=MASTER;Trusted_Connection=True");
like image 149
Dumitrescu Bogdan Avatar answered Nov 04 '22 06:11

Dumitrescu Bogdan


Try changing it to this:

SqlConnection con = new SqlConnection("Server=NEPOLEON\\ADMN;Database=MASTER;Trusted_Connection=True");

or this

SqlConnection con = new SqlConnection(@"Server=NEPOLEON\ADMN;Database=MASTER;Trusted_Connection=True");

In .NET you can escape special characters by either putting an @ in front of the string or using a special value to represent the character you want to escape. In this case you can use \\ to represent a \

like image 36
Abe Miessler Avatar answered Nov 04 '22 06:11

Abe Miessler