Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Connection String Visual Studio 2013 database -- unrecognized escape sequence

My connection string is:

Data Source=MAX-PC\SQLEXPRESS;Initial Catalog=newSchool;Integrated Security=True

but whenever I write :

SqlConnection conn = new SqlConnection("Data Source=MAX-PC\SQLEXPRESS;Initial Catalog=newSchool;Integrated Security=True");

it gives me an error

unrecognized escape sequence

under the \ in Max-PC\SQLEXPRESS

like image 523
user3100088 Avatar asked Mar 10 '26 14:03

user3100088


2 Answers

\ is a speacial character to create escape sequences. you can use \\ or you can put '@' beginning of your connection string to ignore escape characters

var conn = new SqlConnection(@"Data Source=MAX-PC\SQLEXPRESS;Initial Catalog=newSchool;Integrated Security=True");
like image 154
Selman Genç Avatar answered Mar 12 '26 03:03

Selman Genç


C# will understand the '\S' as an escape character. The correct would be double back-slash, or the use of @ before the opening ".

SqlConnection conn = new SqlConnection("Data Source=MAX-PC\\SQLEXPRESS;Initial Catalog=newSchool;Integrated Security=True");

or

SqlConnection conn = new SqlConnection(@"Data Source=MAX-PC\SQLEXPRESS;Initial Catalog=newSchool;Integrated Security=True");
like image 32
Paulo Correia Avatar answered Mar 12 '26 02:03

Paulo Correia