0

I'm on the task writing a function that get input string and convert it into INSERT QUERY such as

INSERT INTO MY_TABLE VALUES (some_random_production_description_here);

And the goal is: I have to keep all the original text for searching and filtering methods/queries

I spent days googling on the internet but I could not figure out this problem. I have a string contains UTF8 and special characters, and I want to insert that string into MySQL database. I tried to use HtmlEncode, UrlEncode, many other kinds of encoding but it seems I cannot GET RID of special characters like single quote (') and forward slash "\" because they are escape sequences while I want to KEEP UTF8 characters.

Everyday I got new exception cases... I know that I can write my new function and keep upgrading it, of course. But the goal I'm looking for is something stable and I'm not going to re-invent the wheel. Anyone has experience on this?

Nhan Tran
  • 113
  • 2
  • 8
  • 7
    How about using parametrized query? [Why do we always prefer using parameters in SQL statements?](https://stackoverflow.com/questions/7505808/why-do-we-always-prefer-using-parameters-in-sql-statements) – Lukasz Szozda Jun 15 '18 at 07:19
  • 2
    to add on @LukaszSzozda comment [SqlParameter](https://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlcommand.parameters(v=vs.110).aspx) – Mike Jun 15 '18 at 07:23
  • I know the Parametrized Query, but just for my case, I have a csv file contains thousands record that need to be imported into the DB. And I tried to convert it into a single INSERT command like "INSERT INTO MY_TABLE VALUES (row_1), (row_2), (row_n);" and I got the problem with string text in it. Maybe my approaching is not correct, that's why I narrow down the scope, and just asking for the encoding method only. Thank you guys – Nhan Tran Jun 15 '18 at 07:45
  • 1
    In that case you may want to use the bulk inserts with a bcp format file, you'll have better luck. Or load it all to a data table and then use SqlBulkCopy – pinkfloydx33 Jun 15 '18 at 09:16

0 Answers0