Hi,
I wish to connect my sqlexpress2016 database by code.
For that I try to build a connection string, but it's giving error ..
Any guidance's will be helpful.static String MyDBConStr = "Data Source=.\sqlexpress; Initial Catalog=AddressDb; Integrated Security=True";
Error : .\sqlexpress class system.string Represnts text as a series of Unicode characters Unrecognized escape sequence
Thanks Regards
PARAMAN
What I have tried:
Build Database connection string
解决方案Do you have an escape character in the middle of the string (\)... 2.4.4.5 String literals (C#)[^]
You have two options:
1. Double the escape char .\\sqlexpress
2. Use @ to disable escape character for this string only @"..."
'\' is a special character in C# - it indicates that the following character is a "special code" and should be combined to produce a new character. For example, "\n" is a newline, "\"" is a double quote, and "\\" is a backslash.
Try this:
static String MyDBConStr = "Data Source=.\\sqlexpress; Initial Catalog=AddressDb; Integrated Security=True";Or better, prefix teh strign with '@' to turn off backslashes:
static String MyDBConStr = @"Data Source=.\sqlexpress; Initial Catalog=AddressDb; Integrated Security=True";
But you shouldn't do that anyway: connection strings should be stored in a configuration file, so you don't have to recompile your application for each new release or change in the DB server!