Click here to Skip to main content
15,902,299 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi all i get the unrecognised esscape sequence in the followeing code
""SqlConnection cn = new SqlConnection("Data Source=B4ITCCTVSERVER\SQLEXPRESS;Initial Catalog=login1;Integrated Security=True");""
i get it on the \ in (B4ITCCTVSERVER(\)SQLEXPRESS)
if i remove the \ it compiles but the program doesnt start as soon as i try to connect to the database i get a error, is there a way to remove the \ and not change the directory path?
Posted
Comments
[no name] 23-Jul-15 8:15am    
Use this: B4ITCCTVSERVER\\SQLEXPRESS

...and read this why and how: https://msdn.microsoft.com/de-de/library/362314fe(v=vs.120).aspx[^]

It means the compiler does not recognise \S as an escape sequence[^]
Either use B4ITCCTVSERVER\\SQLEXPRESS or precede the entire string with @ which makes the string "verbatim" - see string reference[^]
 
Share this answer
 
v5
Comments
Thomas Daniels 23-Jul-15 8:18am    
+5.
Sergey Alexandrovich Kryukov 23-Jul-15 15:26pm    
Sure, a 5.
—SA
The reason that you get this error, is because character combinations that start with a backslash in C# string literals are escape sequences[^]. The compiler finds the escape sequence \S in your string, doesn't recognize it, and gives an error.

There are two possible way to resolve it:

  1. Replace the backslash by its escape sequence, a double backslash:
    C#
    SqlConnection cn = new SqlConnection("Data Source=B4ITCCTVSERVER\\SQLEXPRESS;Initial Catalog=login1;Integrated Security=True");

  2. Make your string literal a verbatim string literal, then the string gets read literally and you don't have to escape the backslash. To make your string literal verbatim, add an @ sign before the literal:
    C#
    SqlConnection cn = new SqlConnection(@"Data Source=B4ITCCTVSERVER\SQLEXPRESS;Initial Catalog=login1;Integrated Security=True");
 
Share this answer
 
v3
Comments
CHill60 23-Jul-15 8:19am    
+5 also. Note you had the same problem as me with the editor here escaping the \\ to \ :-D
jamesmc1535 23-Jul-15 8:22am    
lol XD visual studios is against me and you because its the 2nd error me and you have in common
Thomas Daniels 23-Jul-15 8:30am    
Hmm... weird enough, when I replace it by 4 backslashes, it appears as 4, and when I change it back to 2, it appears as 2... well, at least it displays it correctly now :P
CHill60 23-Jul-15 8:33am    
See my solution ... 5 attempts to get the formatting right! :facepalm:
jamesmc1535 23-Jul-15 8:26am    
okay it works (
\\ ) and the @ but i get another syntax error -
SqlCommand cmd = new SqlCommand("from user1 where username = '" + textBox1.Text + "' and password = '" + textBox2.Text + "'", cn); on the from .any fixes? or advice
jamesmc1535 wrote

C#
okay it works (
\\ ) and the @ but i get another syntax error -
SqlCommand cmd = new SqlCommand("from user1 where username = '" + textBox1.Text + "' and password = '" + textBox2.Text + "'", cn);
on the from .any fixes? or advice
In addition to what you've been already told in some of the comments to Solution 2:

Your approach is wrong from the very beginning. The query composed by concatenation with strings taken from UI. Not only repeated string concatenation is inefficient (because strings are immutable; do I have to explain why it makes repeated concatenation bad?), but there is way more important issue: it opens the doors to a well-known exploit called SQL injection.

This is how it works: http://xkcd.com/327.

Are you getting the idea? The string taken from a control can be anything, including… a fragment of SQL code.

What to do? Just read about this problem and the main remedy: parametrized statements: http://en.wikipedia.org/wiki/SQL_injection.

With ADO.NET, use this: http://msdn.microsoft.com/en-us/library/ff648339.aspx.

Please see my past answers for some more detail:
EROR IN UPATE in com.ExecuteNonQuery();,
hi name is not displaying in name?.

—SA
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900