regex - sql ce escape '(single quote) C# -


I am writing some code to store names in the database. I restrict the names of only a few characters, but Last name is a challenge. Since some people have single quotes in their name (example obrian), so I need to allow this. That's why I have written that to replace 'one' with the place of a regex which I had assumed, 'a literal as far as substitution goes, it works, but it is still the end of the string , And I get an error

There was an error parsing the query. [Token Line Number = 1, Token Line offeset = 71, Token = Brian in Error]

I understand the error, the single quote marks the end of the string, which would enter the other string except Brian Out of quote

I am using the code:

  reggax reg = new regenges ("\"); FirstName = reg.Replace (first name, "\\ '"); LastName = reg.Replace (lastName, "\\ '):  

Then the selection is created with the query string.

  sqlInsertObj.CommandText = String.Format ("Include child name id (child 's name), childLName values ​​(' {0} ',' {1} ')", FNName, lName; sqlInsertObj.ExecuteNonQuery ();  

This name works for any entry other than any entry.

It is better to use SQL queries with parameters, they become string combinations Instead of giving documentation and an example can be found.

If you insist on string combinations, then instead of double singly quote, avoid it.

  firstname = Firstname.Replace ("',' '' ');  

Comments