Click here to Skip to main content
15,892,161 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Please help. It's been hours and still I can't solve this error.

My query is like this:
"INSERT INTO image (document_id, image_dir, page_number) VALUES( '2018-002', 'D:\\DRS\\ARTICLES\\IMAGE\\', 1)"

document_id and image_dir are VARCHAR and page_number is an INT

the error message was:
Quote:
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''D:\DRS\ARTICLES\IMAGE\', 1)' at line 1


What I have tried:

I already tried putting ` as column-name delimiter but still to no avail.
Posted
Updated 11-Oct-18 20:07pm

1 solution

It's because you have a combination of SQL and C# character escapes going on.
'\' is a special character in C#: it says "the next character is to be treated as a character, regardless of what it is" and allows us to - for example - insert a double quote into a string:
C#
string s = "Here\"s Johnny!";
So to insert a backslash into a C# string we have to escape the backslash itself:
C#
string t  = "back\\slash";
But ... MySql also uses backslash as an escape character: MySQL :: MySQL 8.0 Reference Manual :: 9.1.1 String Literals[^]
So in order to insert a backslash into a MySql string from C#, you have to allow for both escape systems:
C#
string u = "INSERT INTO MyTable (MyColumn) VALUES ('D:\\\\temp\\\\filename.txt')";
Or better - pass it as a parameter so the whole horrible mess is mypassed ...
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900