John
John

Reputation: 2141

connection string not working

using (SqlConnection conn = new SqlConnection("Data Source=SARAN-PC\SQLEXPRESS;Initial Catalog=ERP;integrated security=SSPI"))
{}

when i include this line in asp.net web application,it shows..(Unrecognised escape sequence as error) locating \S in the connection string... help me to connect.. Thanks a lot

Upvotes: 1

Views: 464

Answers (4)

Miroslav Zadravec
Miroslav Zadravec

Reputation: 3730

All answers are correct but you shouldn't hardcode this string. Read it from resource or configuration file.

Upvotes: 0

VdesmedT
VdesmedT

Reputation: 9113

Add the @ sign before the start of your string to ignore escape sequences.

new SqlConnection(@"Data Source=SARAN-PC\SQLEXPRESS;Initial Catalog=ERP;integrated security=SSPI")) 

Upvotes: 2

Alex Reitbort
Alex Reitbort

Reputation: 13696

using (SqlConnection conn = new SqlConnection("Data Source=SARAN-PC\\SQLEXPRESS;Initial Catalog=ERP;integrated security=SSPI")) {}

Or

using (SqlConnection conn = new SqlConnection(@"Data Source=SARAN-PC\SQLEXPRESS;Initial Catalog=ERP;integrated security=SSPI")) {}

Upvotes: 2

DavidGouge
DavidGouge

Reputation: 4623

It's the "\" in your string that C# is interpreting as an escape sequence (for example a carriage return).

You can either:

using (SqlConnection conn = new SqlConnection(@"Data Source=SARAN-PC\SQLEXPRESS;Initial Catalog=ERP;integrated security=SSPI")) {}

Note the @ symbol.

or, you could use a double backslash:

using (SqlConnection conn = new SqlConnection("Data Source=SARAN-PC\\SQLEXPRESS;Initial Catalog=ERP;integrated security=SSPI")) {}

Upvotes: 2

Related Questions