468,103 Members | 1,172 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,103 developers. It's quick & easy.

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

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
Nov 16 '05 #1
5 7678
Steven C wrote:
conn = new SqlConnection("Data Source=SWC_K7\CHRISMON1;


Add the "add" character in front of the string:

conn = new SqlConnection(@"Data Source=SWC_K7\CHRISMON1;

--
Greetings
Jochen

Do you need a memory-leak finder ?
http://www.codeproject.com/tools/leakfinder.asp

Do you need daily reports from your server?
http://sourceforge.net/projects/srvreport/
Nov 16 '05 #2
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");

<Steven C> wrote in message
news:ld********************************@4ax.com...
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

Nov 16 '05 #3
The backslash is the escape character. Type "SWC_K7\\CHRISMON1" in the
connection string.
<Steven C> wrote in message
news:ld********************************@4ax.com...
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

Nov 16 '05 #4
<Steven C <>> wrote:
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.


See

http://www.pobox.com/~skeet/csharp/faq/#escapes
and
http://www.pobox.com/~skeet/csharp/f...batim.literals

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #5
Awesome!

This worked.

Thanks, gentlemen

Steven C
Nov 16 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by /dev/null | last post: by
1 post views Thread by Lyn | last post: by
7 posts views Thread by ryan.fairchild | last post: by
4 posts views Thread by Peter | last post: by
5 posts views Thread by Prince Kumar | last post: by
2 posts views Thread by Tim Marshall | last post: by
reply views Thread by Jeff Groves | last post: by
1 post views Thread by Solo | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.