Unrecognized escape sequence in SqlConnection constructor
Asked Answered
G

2

7

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");
Gean answered 25/9, 2012 at 15:22 Comment(1)
nest time when you ask question fromat it proper to get proper answer and avoid negetive votes...its edited by me now..Bismuthinite
M
23

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");
Melloney answered 25/9, 2012 at 15:23 Comment(0)
T
8

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 \

Tallou answered 25/9, 2012 at 15:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.