简体   繁体   中英

Cant save '\r\n' tag from Enviroment.NewLine into database

My string looks like this:

string output = input + Enviroment.NewLine; // output == text\r\n

I saving it to database like this:

this.db.Table.Add(new Result { Output = output});
this.db.SaveChanges();

And when i look into SQL Object Explorer , Enviroment.NewLine is not saved into database (in Output column i get "text" instead of "text\\r\\n" . My question is, how to save string with NewLine tag into my database? ( Output is string property in DB). While im debugging, output is properly stored as "text\\r\\n"

EDIT

Ok, i get it, in my database my string looks like this:

1 text
2

Instead of

1 text\r\n

So how to save it as raw tag?

\\n is a character. This character has the value 10 if converted to an int:

Console.WriteLine(@"\n - " + ((int)'\n'));

The question is, what do you want to store in your DB?

If you want to save a test like this

abc\\ncde

Or like this

abc
cde

Printing out your strings to the console will show you what you store in your db:

Console.WriteLine("abc\ncde"); // This output is
Console.WriteLine("abc" + ((char)10) + "cde"); // equal to this

// If you want to store a backslash ('\') you have to escape it:
Console.WriteLine("abc\\ncde"); // This output is
// Or tell C# to ignore escape-sequences in this string by adding an @:
Console.WriteLine(@"abc\ncde"); // equal to this

We see now that "\\n" is only Visual-Studios way to show you a NewLine (char 10).

Use CHAR(13) , hope it will work for you like below code:-

DECLARE @text NVARCHAR(100)
SET @text = 'This is line 1.' + CHAR(13) + 'This is line 2.'
SELECT @text

If you are using some web application you have to save the <br/> (Html tags)into database directly.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM