Storing a string with double and single quotes from EF to SQL

The focus is on the format string text

, everything else is just for context. The actual line is about 10 pages, if that matters.

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 deal with storing single and double quoted string from EF to SQL? If not, what is the way to do it? db

is just our dbContext.

Edit: It might not have been clear from the question. I'm worried that when I issue a query in SQL Server or execute a SQL command from C #, I cannot enter a string with nothing in single quotes without doubling them. However, my question is whether EF is smart enough to store the single quoted string or whether it needs to be doubled.

+3


source to share


2 answers


Getting from .NET to SQL is an EF issue to worry about. The rules for what characters are special and how to avoid them vary from database to database, but in each case EF has code to handle.

So you don't have to worry about what '

is special in SQL in general.

All you need to worry about is writing the correct string in .NET. If you use @

before a string to have a string literal, then all characters are treated as -is, except for what "

is executed as ""

.

Without, you are not allowed newlines or quotes, but you can avoid them using the following escape sequences:

  • \u

    followed by four hexadecimal digits: the character with that code point
  • \u

    followed by eight hexadecimal digits: the character with that code point.
  • \x

    followed by one to four hexadecimal digits: the character with that code point.
  • \a

    just like \u0007

    (call)
  • \b

    same as \u0008

    (backspace)
  • \f

    same as \u000C

    (form feed)
  • \n

    the same as \u000A

    (newline)
  • \r

    same as \u000D

    (carriage return)
  • \t

    same as \u0009

    (tab)
  • \v

    same as \u000B

    (vertical tab)
  • \'

    just like \u0027

    (apostrophe)
  • \"

    same as \u0022

    (quote)
  • \\

    the same as \u005C

    ()
  • \0

    same as \u0000

    (null character)

Not all of them are needed in strings, so you can use '

instead \'

, but they are either allowed to be hard to print, which is difficult 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.
";

      

Same as:

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

      



Or perhaps like:

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

      

As it is not clear with SO markup, do you need spaces or tabs after the first new line.

All the same, but if the reason you have ''

is to escape for SQL then you shouldn't, leave it in EF to worry about. If you have:

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";

      

It's just a question that is easier for you to write and read about.

Generally, I would recommend that you use the latter form most of the time, but the verbatim ( @

) form in cases where there are many newlines in the text or where there \

are many characters (regular expressions and Windows file paths, for example).

+3


source


No need to worry about SQL syntax, this is one of the main advantages of using EF or any ORM. If you have a line:

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

      



When you store this string with EF in the database, it will look like "Don't worry about single quotes."

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

+3


source







All Articles