0

My TSQL query return a string like this :

a\path\to\file.xml

And I want to put it in a string like this :

string path = reader.GetString(0); (return the query result of the first field => "a\path\to\file.xml")

so when I want to use path I have got this :

Console.WriteLine(path); //Output : `apathtofile.xml`

But I want the value like : a\path\to\file.xml.

I already try this because i think I have to double the backslash in the string:

path = reader.GetString(0).Replace("\\", "\\\\");  -> do nothing

path = reader.GetString(0).Replace(@"\", @"\\\"); -> do nothing

path = Regex.Replace(reader.GetString(0), "\\", "\\\\") -> analyse de "\" - \ non conforme à la fin du modèle 

path = Regex.Replace(reader.GetString(0), @"\", @"\\\") -> analyse de "\" - \ non conforme à la fin du modèle 

French installation -> french error message. sory

CrunchyArtie
  • 417
  • 5
  • 18
  • 3
    I don't understand. You get this `a\path\to\file.xml` string with `reader.GetString(0)` method? If so, `Console.WriteLine` doesn't write it as `apathtofile.xml` IMO.. Totally unclear what you have and what have try to do. – Soner Gönül Feb 11 '15 at 10:24
  • The question is very unclear at the moment - it doesn't look like you've got *any* backslashes in the string at the moment... and if you did have, the first result would double them, although it's not clear why you want to... – Jon Skeet Feb 11 '15 at 10:25
  • 1
    Why your TSQL returns: `a\path\to\file.xml` and WriteLine print: `apathtofile.xml`? – BendEg Feb 11 '15 at 10:25
  • My query in sql server management studio return `"a\path\to\file.xml"` value, The field type is a string. In my console application I want to get the same value but when I put the value in `string path` the "\" does not interpreted like a char but like special char for something like "\n" so I want to change "\" to "\\" for, at final, I will have a value like `"a\path\to\file.xml"`and not like `"apathtofile.xml"` – CrunchyArtie Feb 11 '15 at 10:38
  • @Crunchy_Artie The **only** time when you have to escape a character in a string is if you use a string literal, not when you already have a string object. – Dirk Feb 11 '15 at 10:45

3 Answers3

0

You have to options make a double backslash from a single backslash:

  1. string test = "your\\string".Replace("\\", "\\\\");
  2. string test = "your\\string".Replace(@"\", @"\\");
BendEg
  • 20,098
  • 17
  • 57
  • 131
  • I'm sorry may be I am not clear but in your example yo have `"my\\string"` but me I have just `"my\string"` so Replace does not work because I think this pattern : `"\\"` never match and this pattern : `"\"` it's impossible because in this case he want to use `\"` – CrunchyArtie Feb 11 '15 at 10:39
  • Your confusing what comes from the database and what is in your code. In the c# code, \\ ist what the code contains, but in memory is only \. Thats the same as in your database. Thats because backslash is also an escape char in code. – BendEg Feb 11 '15 at 10:44
0

The query was created by another developer and he wrote this into the query:

REPLACE(SubQuery.PATH,'/','\')

I just change it to :

REPLACE(SubQuery.PATH,'/','\\')

And it's work fine.

CrunchyArtie
  • 417
  • 5
  • 18
0

if i undrstand it correctly- use any split function and then use cursor to rebuild the string

--CREATE  TABLE tstr(str varchar(10))
INSERT INTO tstr
SELECT * FROM [dbo].[fnSplitString]('a\path\to\file.xml','\') AS fss
DECLARE @x varchar(20)=(SELECT TOP 1 str from tstr)
SELECT * FROM tstr
DECLARE   cur CURSOR
FOR SELECT str
FROM [dbo].[tstr] AS t
DECLARE @i varchar(10)
DECLARE @a varchar(100)=''
OPEN cur
FETCH NEXT FROM cur INTO @i

WHILE @@FETCH_STATUS=0
BEGIN
SET @a=@a+@i+'\\'   
FETCH NEXT FROM cur INTO @i
END
SELECT left(@a,len(@a)-2)

CLOSE cur
DEALLOCATE cur
TRUNCATE TABLE [dbo].[tstr]
Dudi Konfino
  • 1,126
  • 2
  • 13
  • 24