C# 如何将具有反斜杠的连接字符串传递给 SqlConnection?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10542565/
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 14:14:31  来源:igfitidea点击:

How to pass connection string that has a backward slash to SqlConnection?

c#sqlsql-serverado.net

提问by user1104946

I am trying to call the stored procedure using C#.

我正在尝试使用 C# 调用存储过程。

I am facing problem in the following line.

我在以下行中遇到问题。

SqlConnection("Server=DB2\XPT;DataBase=SCFHP_EDI_P1;Integrated Security=SSPI");

The part that I am not able to use is the server DB2\XPT.

我无法使用的部分是 server DB2\XPT

What do I need to do to use server name as DB2\XPT?

我需要做什么才能使用服务器名称DB2\XPT

采纳答案by Rapha?l Althaus

("Server=DB2\XPT;DataBase=SCFHP_EDI_P1;Integrated Security=SSPI");

or

或者

(@"Server=DB2\XPT;DataBase=SCFHP_EDI_P1;Integrated Security=SSPI")

回答by Rapha?l Althaus

You need to escape the backward slash \in your connection string or use the @symbol if you want to avoid escaping characters in your string.

如果要避免在字符串中转义字符,则需要对\连接字符串中的反斜杠进行转义或使用该@符号。

Read more about it on MSDN.

在 MSDN 上阅读有关它的更多信息。

Corrected syntax 1 using @ symbol:

使用 @ 符号更正了语法 1:

SqlConnection(@"Server=DB2\XPT;DataBase=SCFHP_EDI_P1;Integrated Security=SSPI");

Corrected syntax 2 using escaping:

使用转义更正语法 2:

SqlConnection("Server=DB2\XPT;DataBase=SCFHP_EDI_P1;Integrated Security=SSPI");