How Do I "Escape" A SQL Connect String With Non-Standard Server Name?

  • Thread starter Thread starter Steven C
  • Start date Start date
S

Steven C

Hello!

I am trying to connect to a SQL Server (MSDE) database in mixed mode
authentication, via C#, but when I use the MSDE instance name, I keep
getting an "Unrecognized Escape Sequence" error. The Data Source name
has a backslash in it, and I'm guessing that this is causing the
problem. How do I "Escape" the name, to make this work? I'm able to
connnect in the .NET developement platform (Tools / Connect To
Database), etc, using the full MSDE instance name, ie,
SWC_K7\CHRISMON1, but when I call that from code, the compiler catches
it as an error.

Connect String:

conn = new SqlConnection("Data Source=SWC_K7\CHRISMON1;
User Id=yadda;
password=yadda;
Initial Catalog=ChrismonContacts");

Thanks!

Steven
 
Steven:

In C#, using the "@" symbol in front of a string wills escape it. There are
many other instances, particularly when dealing with Paths, Files, Regular
Expressions that this is very handy. You can either replace each single
instance of "\" with "\\" or use the @ Symbol: conn = @"Data
Source=SWC_K7\CHRISMON1;User Id=yadda; password=yadda;
Initial Catalog=ChrismonContacts");
 
The backslash is the escape character. Type "SWC_K7\\CHRISMON1" in the
connection string.
 
Back
Top