Search code examples
c#stringsqlparameter

Unrecognized escape sequence when using a path as a SQL parameter


I am trying to store a string or nvarchar(500) in SQL. When I pass a full file path as a string parameter, there is an error unrecognized escape sequence.

Since path is not an usual param that this stored procedure expects, how can I open this possibility so it can accept string such as c:\foldername\subfoldername. Am I suppose to add @ at the begging of a string or use a StringBuilder?

Thanks


Solution

  • Since backslash is considered as a special character(escape), it's causing the issue. Use / or \\ in the path as:

          c:/foldername/subfoldername
          c:\\foldername\\subfoldername
    

    OR as you said, use @ in the front as :

         @"c:\foldername\subfoldername"
    

    EDIT: For Javascript, I will simply replace the \ to / as below:

         path = path.split("\\").join("/");