第1行''D:\ DRS \ARTICLES \IMAGE \',1)'附近的C#mysql ERROR 1064语法错误 [英] C# mysql ERROR 1064 syntax error near ''D:\DRS\ARTICLES\IMAGE\', 1)' at line 1

查看:92
本文介绍了第1行''D:\ DRS \ARTICLES \IMAGE \',1)'附近的C#mysql ERROR 1064语法错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请帮忙。已经好几个小时了,我仍然无法解决这个错误。



我的查询是这样的:

INSERT INTO image(document_id, image_dir,page_number)VALUES('2018-002','D:\\DRS \\ARTICLES \\IMAGE \\',1)



document_id和image_dir是VARCHAR,page_number是INT



错误信息是:

Quote:

您的SQL语法中有错误;查看与您的MySQL服务器版本对应的手册,以便在第1行D:\ DRS \ARTICLES \ IMAGE \',1)'附近使用正确的语法





我尝试了什么:



我已经尝试将`作为列名分隔符但仍然没有用。

解决方案

这是因为你有一个SQL和C#字符转义的组合。

'\'是一个C#中的特殊字符:它表示下一个字符将被视为字符,无论它是什么,并允许我们 - 例如 - 在字符串中插入双引号:

< pre lang =c#> string s = 这里\s Johnny!;

因此,要在C#字符串中插入反斜杠,我们必须转义反斜杠本身:

  string  t =   back\\slash; 

但是...... MySql也使用反斜杠作为转义字符: MySQL :: MySQL 8.0参考手册:: 9.1.1字符串文字 [ ^ ]

因此,为了从C#向MySql字符串插入反斜杠,您必须允许两个转义系统:

  string  u =   INSERT INTO MyTable(MyColumn)VALUES('D:\ \\\\\\\\filename.txt'); 

或者更好 - 将它作为一个参数传递,这样整个可怕的混乱都被重复了..


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.

解决方案

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:

string s = "Here\"s Johnny!";

So to insert a backslash into a C# string we have to escape the backslash itself:

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:

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 ...


这篇关于第1行''D:\ DRS \ARTICLES \IMAGE \',1)'附近的C#mysql ERROR 1064语法错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆