C# SqlConnection 构造函数中无法识别的转义序列

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12586083/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 23:49:18  来源:igfitidea点击:

Unrecognized escape sequence in SqlConnection constructor

c#sqlconnection

提问by Pranav Mishra

when ever I make connection it gives an error

当我建立连接时,它会出错

Unrecognized escape sequence.

无法识别的转义序列。

NEPOLEON\ADMN HERE ON THIS SLASH. NEPOLEON\ADMN IS MY SQL SERVER NAME.

SqlConnection con = new SqlConnection("Server=NEPOLEON\ADMN;Database=MASTER;Trusted_Connection=True");

采纳答案by Dumitrescu Bogdan

Escape the \ character like :

转义 \ 字符,如:

SqlConnection con = new SqlConnection("Server=NEPOLEON\ADMN;Database=MASTER;Trusted_Connection=True");

or

或者

SqlConnection con = new SqlConnection(@"Server=NEPOLEON\ADMN;Database=MASTER;Trusted_Connection=True");

回答by Abe Miessler

Try changing it to this:

尝试将其更改为:

SqlConnection con = new SqlConnection("Server=NEPOLEON\ADMN;Database=MASTER;Trusted_Connection=True");

or this

或这个

SqlConnection con = new SqlConnection(@"Server=NEPOLEON\ADMN;Database=MASTER;Trusted_Connection=True");

In .NET you can escape special characters by either putting an @in front of the string or using a special value to represent the character you want to escape. In this case you can use \\to represent a \

在 .NET 中,您可以通过@在字符串前面放置一个或使用特殊值来表示要转义的字符来转义特殊字符。在这种情况下,您可以使用\\来表示\