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

后端 未结 2 1763
南旧
南旧 2021-01-18 13:37

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

I am facing problem in the following line.

SqlConnection(\"Server=DB2\\XPT;DataBase=SCFHP_EDI_P1;         


        
相关标签:
2条回答
  • 2021-01-18 13:58

    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.

    Corrected syntax 1 using @ symbol:

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

    Corrected syntax 2 using escaping:

    SqlConnection("Server=DB2\\XPT;DataBase=SCFHP_EDI_P1;Integrated Security=SSPI");
    
    0 讨论(0)
  • 2021-01-18 14:15
    ("Server=DB2\\XPT;DataBase=SCFHP_EDI_P1;Integrated Security=SSPI");
    

    or

    (@"Server=DB2\XPT;DataBase=SCFHP_EDI_P1;Integrated Security=SSPI")
    
    0 讨论(0)
提交回复
热议问题