无法将Enviroment.NewLine中的'\ r \ n'标签保存到数据库中

时间:2019-04-26 09:30:47

标签: c# entity-framework linq

我的字符串如下:

use Pod::Load;
my $pod = load( $*PROGRAM );
say $pod.perl;  # $[]

我将其保存到数据库中,如下所示:

config.i18n.load_path += Dir[Rails.root.join('config/locales/**/*.yml').to_s]

当我查看string output = input + Enviroment.NewLine; // output == text\r\n 时,this.db.Table.Add(new Result { Output = output}); this.db.SaveChanges(); 未保存到数据库中(在SQL Object Explorer列中,我得到的是Enviroment.NewLine而不是Output。我的问题是,如何将带有"text"标签的"text\r\n"保存到我的数据库中(string是数据库中的字符串属性)。在即时调试时,NewLine被正确存储为{{1} }

编辑

好吧,我明白了,在我的数据库中,我的字符串如下:

Output

代替

output

那么如何将其保存为原始标签?

2 个答案:

答案 0 :(得分:1)

\n是一个字符。如果转换为整数,则此字符的值为10:

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

问题是,您想在数据库中存储什么?

如果您要保存这样的测试

  

abc \ ncde

或者这样

  

abc
  cde

将字符串打印到控制台将显示您存储在数据库中的内容:

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

我们现在看到“ \ n”只是Visual Studio向您显示换行符的方式(字符10)。

答案 1 :(得分:1)

使用CHAR(13),希望它能像下面的代码一样为您工作:-

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

如果您正在使用某些Web应用程序,则必须将<br/>(HTML标签)直接保存到数据库中。