保存从EF到SQL的双引号和单引号的字符串

时间:2022-09-15 13:02:36

The focus of the question is the string text format, everything else is just there for context. The actual string is about 10 pages long, if that matters.

问题的焦点是字符串文本格式,其他所有内容都只针对上下文。如果有关系的话,实际的字符串大约有10页长。

string text = @"
    "" My string ''stringy'' with lots of ''quoties'' "" said Jimmy ''The Jimminator'' Smith.
";

API.Models.Table seedTable = new API.Models.Table()
{
    Created = new DateTimeOffset(DateTime.Now),
    TableText = text
};

db.Table.AddOrUpdate(seedTable);
db.SaveChanges();

Is this the correct way to handle saving a string with single and double quotes from EF to SQL? If not, what's the proper way to do it? db is just our dbContext.

这是处理从EF到SQL使用单引号和双引号保存字符串的正确方法吗?如果没有,正确的方法是什么?db只是我们的dbContext。

Edit: This might not have been clear from the question. My concern is that when I issue a query in SQL Server or do a SQL command from C#, I cannot enter a string with anything in single quotes without doubling them up. However, my question is whether EF is somehow smart enough to save a string with single quotes or needs to double them up.

编辑:这个问题可能不太清楚。我关心的是,当我在SQL Server中发出查询或从c#执行SQL命令时,我无法在单引号中输入任何内容,而不会使它们加倍。然而,我的问题是,EF是否足够聪明,能够保存带有单引号的字符串,还是需要将它们加倍。

2 个解决方案

#1


3  

Getting from .NET to SQL is EF's problem to worry about. The rules of just what characters are special and how to escape them vary from database to database, but in each case EF has code to handle that.

从。net到SQL是EF需要担心的问题。在不同的数据库中,特定的字符和如何转义字符的规则各不相同,但是在每种情况下,EF都有代码来处理这些字符。

So you don't need to worry about ' being special in SQL at all.

因此,您根本不需要担心“在SQL中是特殊的”。

All you need to worry about therefore is how to write a valid string in .NET. If you use @ before a string to have a verbatim string literal, then all characters are treated as-is, with the exception of " being escaped as "".

因此,您需要担心的是如何在。net中编写一个有效的字符串。如果您在字符串前使用@来具有逐字字符串字面量,那么所有字符都按原样处理,除了“转义为”之外。

Without, you aren't allowed newlines or quote marks, but can escape them using the following escapes:

没有的话,就不允许使用换行符或引号,但是可以使用以下转义符来转义:

  1. \u followed by four hexadecimal digits: The character with that code-point
  2. \u后面跟着四个十六进制数字:带有那个代码点的字符
  3. \U followed by eight hexadecimal digits: The character with that code-point.
  4. \U后面跟着八个十六进制数字:带有那个代码点的字符。
  5. \x followed by one to four hexadecimal digits: The character with that code-point.
  6. \x后面跟着一个到四个十六进制数字:带有那个代码点的字符。
  7. \a same as \u0007 (bell)
  8. \a同\u0007(铃声)
  9. \b same as \u0008 (backspace)
  10. \b相同\u0008 (backspace)
  11. \f same as \u000C (form feed)
  12. \f = \u000C(表格输入)
  13. \n same as \u000A (newline)
  14. \n与\u000A(换行)
  15. \r same as \u000D (carriage return)
  16. \r与\u000D(回车)
  17. \t same as \u0009 (tab)
  18. \t与\u0009(附档)
  19. \v same as \u000B (vertical tab)
  20. \v和\u000B(垂直标签)
  21. \' same as \u0027 (apostrophe)
  22. \u0027(撇号)
  23. \" same as \u0022 (quotation mark)
  24. \“与\u0022(引号)相同
  25. \\ same as \u005C ()
  26. \\ u005C ()
  27. \0 same as \u0000 (null character)
  28. \0 = \u0000(空字符)

Not all of these are necessary in strings, so you can use ' instead of \' but they are allowed either as hard to type, hard to distinguish (how to tell a tab from some spaces?) or not allowed in other contexts (you need \' in character literals).

并非所有这些都是字符串中必需的,所以您可以使用'而不是\',但它们可以是打字难、区分难(如何从某些空格中区分标签?),也可以是在其他上下文中不允许的(在字符文本中需要')。

Your example:

你的例子:

string text = @"
    "" My string ''stringy'' with lots of ''quoties'' "" said Jimmy ''The Jimminator'' Smith.
";

Is the same as:

是一样的:

string text="\n    \" My string ''stringy'' with lots of ''quoties'' \" said Jimmy ''The Jimminator'' Smith.\n";

Or perhaps as:

或者为:

string text="\n\t\" My string ''stringy'' with lots of ''quoties'' \" said Jimmy ''The Jimminator'' Smith.\n";

As it's not clear with SO's markup whether you wanted spaces or tabs after the first new-line.

因为不清楚SO的标记在第一行之后是否需要空格或制表符。

Either of those are exactly the same, however if the reason you have '' is to escape for SQL, then you shouldn't, leave it to EF to worry about. With that whether you have:

这两种方法都是完全相同的,但是,如果您拥有的“原因”是为了逃避SQL,那么您不应该这样做,那么应该让EF来操心。如果你有:

string text = @"
    "" My string 'stringy' with lots of 'quoties' "" said Jimmy 'The Jimminator' Smith.
";

Or:

或者:

string text="\n    \" My string 'stringy' with lots of 'quoties' \" said Jimmy 'The Jimminator' Smith.\n";

Is purely a matter of which you find easier to write and read.

纯粹是一个你觉得更容易写和读的问题。

Generally, I'd recommend you use the latter form most of the time, but the verbatim (@) form in cases where either there are a lot of new lines in the text, or where there are a lot of \ characters (regular expressions and Windows file paths, for example).

通常,我建议您在大多数情况下使用后一种形式,但是如果文本中有很多新行,或者有很多\字符(例如正则表达式和Windows文件路径),则使用逐字(@)形式。

#2


3  

Not having to worry about SQL syntax is one of the major benefits of using EF or any ORM for that matter. If you have a string:

不用担心SQL语法,这是使用EF或任何ORM的主要好处之一。如果你有一根绳子:

string myString = "Don't worry about single quotes";

When you store that string using EF to the database, it will go in just like that "Don't worry about single quotes".

当您使用EF将该字符串存储到数据库中时,它就会像“不要担心单引号”一样进入数据库。

No other syntax matters either (i.e. <>,%,etc.)

其他语法也不重要(例如<>,%等)

#1


3  

Getting from .NET to SQL is EF's problem to worry about. The rules of just what characters are special and how to escape them vary from database to database, but in each case EF has code to handle that.

从。net到SQL是EF需要担心的问题。在不同的数据库中,特定的字符和如何转义字符的规则各不相同,但是在每种情况下,EF都有代码来处理这些字符。

So you don't need to worry about ' being special in SQL at all.

因此,您根本不需要担心“在SQL中是特殊的”。

All you need to worry about therefore is how to write a valid string in .NET. If you use @ before a string to have a verbatim string literal, then all characters are treated as-is, with the exception of " being escaped as "".

因此,您需要担心的是如何在。net中编写一个有效的字符串。如果您在字符串前使用@来具有逐字字符串字面量,那么所有字符都按原样处理,除了“转义为”之外。

Without, you aren't allowed newlines or quote marks, but can escape them using the following escapes:

没有的话,就不允许使用换行符或引号,但是可以使用以下转义符来转义:

  1. \u followed by four hexadecimal digits: The character with that code-point
  2. \u后面跟着四个十六进制数字:带有那个代码点的字符
  3. \U followed by eight hexadecimal digits: The character with that code-point.
  4. \U后面跟着八个十六进制数字:带有那个代码点的字符。
  5. \x followed by one to four hexadecimal digits: The character with that code-point.
  6. \x后面跟着一个到四个十六进制数字:带有那个代码点的字符。
  7. \a same as \u0007 (bell)
  8. \a同\u0007(铃声)
  9. \b same as \u0008 (backspace)
  10. \b相同\u0008 (backspace)
  11. \f same as \u000C (form feed)
  12. \f = \u000C(表格输入)
  13. \n same as \u000A (newline)
  14. \n与\u000A(换行)
  15. \r same as \u000D (carriage return)
  16. \r与\u000D(回车)
  17. \t same as \u0009 (tab)
  18. \t与\u0009(附档)
  19. \v same as \u000B (vertical tab)
  20. \v和\u000B(垂直标签)
  21. \' same as \u0027 (apostrophe)
  22. \u0027(撇号)
  23. \" same as \u0022 (quotation mark)
  24. \“与\u0022(引号)相同
  25. \\ same as \u005C ()
  26. \\ u005C ()
  27. \0 same as \u0000 (null character)
  28. \0 = \u0000(空字符)

Not all of these are necessary in strings, so you can use ' instead of \' but they are allowed either as hard to type, hard to distinguish (how to tell a tab from some spaces?) or not allowed in other contexts (you need \' in character literals).

并非所有这些都是字符串中必需的,所以您可以使用'而不是\',但它们可以是打字难、区分难(如何从某些空格中区分标签?),也可以是在其他上下文中不允许的(在字符文本中需要')。

Your example:

你的例子:

string text = @"
    "" My string ''stringy'' with lots of ''quoties'' "" said Jimmy ''The Jimminator'' Smith.
";

Is the same as:

是一样的:

string text="\n    \" My string ''stringy'' with lots of ''quoties'' \" said Jimmy ''The Jimminator'' Smith.\n";

Or perhaps as:

或者为:

string text="\n\t\" My string ''stringy'' with lots of ''quoties'' \" said Jimmy ''The Jimminator'' Smith.\n";

As it's not clear with SO's markup whether you wanted spaces or tabs after the first new-line.

因为不清楚SO的标记在第一行之后是否需要空格或制表符。

Either of those are exactly the same, however if the reason you have '' is to escape for SQL, then you shouldn't, leave it to EF to worry about. With that whether you have:

这两种方法都是完全相同的,但是,如果您拥有的“原因”是为了逃避SQL,那么您不应该这样做,那么应该让EF来操心。如果你有:

string text = @"
    "" My string 'stringy' with lots of 'quoties' "" said Jimmy 'The Jimminator' Smith.
";

Or:

或者:

string text="\n    \" My string 'stringy' with lots of 'quoties' \" said Jimmy 'The Jimminator' Smith.\n";

Is purely a matter of which you find easier to write and read.

纯粹是一个你觉得更容易写和读的问题。

Generally, I'd recommend you use the latter form most of the time, but the verbatim (@) form in cases where either there are a lot of new lines in the text, or where there are a lot of \ characters (regular expressions and Windows file paths, for example).

通常,我建议您在大多数情况下使用后一种形式,但是如果文本中有很多新行,或者有很多\字符(例如正则表达式和Windows文件路径),则使用逐字(@)形式。

#2


3  

Not having to worry about SQL syntax is one of the major benefits of using EF or any ORM for that matter. If you have a string:

不用担心SQL语法,这是使用EF或任何ORM的主要好处之一。如果你有一根绳子:

string myString = "Don't worry about single quotes";

When you store that string using EF to the database, it will go in just like that "Don't worry about single quotes".

当您使用EF将该字符串存储到数据库中时,它就会像“不要担心单引号”一样进入数据库。

No other syntax matters either (i.e. <>,%,etc.)

其他语法也不重要(例如<>,%等)