SQL 语法错误 C#
我对 C# 相当陌生,我正在尝试制作一个教师管理程序。
这是我用来执行查询的函数。
string commentString = "sC" + (y + 1) + "Y" + (i + 1) + "";
executeQuery("UPDATE student SET " +
commentString + " = '" + s.getStudentCourses(i,y,s)+
"' WHERE sNumber = '" + s.getStudNumber(s) + "'");
我的查询字符串:
query "UPDATE student SET 'sComments1-1' = 'wa5235' WHERE sNumber = 68721919" string
我得到的异常:
[MySql.Data.MySqlClient.MySqlException] {"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 ''sComments1-1' = 'wa5235' WHERE sNumber = 68721919' at line 1"} MySql.Data.MySqlClient.MySqlException
这是 SQL 数据结构:
CREATE TABLE `NewTable` (
`sNumber` int(9) NOT NULL ,
`sFirstName` varchar(32) NOT NULL ,
`sLastName` varchar(32) NOT NULL ,
`sDOB` varchar(9) NOT NULL ,
`sGrade` int(1) NOT NULL ,
`sEmail` varchar(32) NULL ,
`sParentName` varchar(32) NOT NULL ,
`sParentPhone` varchar(11) NOT NULL ,
`sHomeAddress` varchar(32) NOT NULL ,
`sComments1-1` varchar(255) NOT NULL ,
使用 MySQL 5.5
我不知道为什么,但这给了我 sql 错误。请帮助我,我的作业两天后到期,我真的需要完成它。
I am fairly new to C#, and i am trying to make a teacher management program.
This is the function that i am using to execute the query.
string commentString = "sC" + (y + 1) + "Y" + (i + 1) + "";
executeQuery("UPDATE student SET " +
commentString + " = '" + s.getStudentCourses(i,y,s)+
"' WHERE sNumber = '" + s.getStudNumber(s) + "'");
My Query String:
query "UPDATE student SET 'sComments1-1' = 'wa5235' WHERE sNumber = 68721919" string
The exception i get:
[MySql.Data.MySqlClient.MySqlException] {"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 ''sComments1-1' = 'wa5235' WHERE sNumber = 68721919' at line 1"} MySql.Data.MySqlClient.MySqlException
Here is the SQL data structure:
CREATE TABLE `NewTable` (
`sNumber` int(9) NOT NULL ,
`sFirstName` varchar(32) NOT NULL ,
`sLastName` varchar(32) NOT NULL ,
`sDOB` varchar(9) NOT NULL ,
`sGrade` int(1) NOT NULL ,
`sEmail` varchar(32) NULL ,
`sParentName` varchar(32) NOT NULL ,
`sParentPhone` varchar(11) NOT NULL ,
`sHomeAddress` varchar(32) NOT NULL ,
`sComments1-1` varchar(255) NOT NULL ,
Using MySQL 5.5
I do not know why, but this is giving me sql errors. Please help me, my assignment is due in 2 days and i really need this finished.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
此处询问并回答您的问题的重复项(列名称中的减号):
MySQL INSERT - 字段名称需要反引号/重音分隔吗?
当在查询中使用带有减号的列名时,您需要在单引号上使用“反引号”。像这样:
A duplicate of your problem (minus signs in column names) is asked and answered here:
MySQL INSERT - Do field names require backtick/accent delimination?
You need to use 'back-ticks' instead on single quotes when using the column name with a minus sign in your query. Like this:
似乎没有任何方法可以从
"sC" + (y + 1) + "Y" + (i + 1) 获取
所以我不确定为什么你认为你的查询字符串的形式正确。sComments1-1
) + ""在任何情况下,您的列名称都需要用反引号而不是单引号引起来:
假设
sComments1-1
是拼写错误或早期版本,并且实际上应该是多个之一 表单sCaYb
的列,其中a
和b
是不同的整数,代码将如下所示:我添加了反引号围绕列名称和从
sNumber
值中删除它们(因为它是整数而不是字符列)。There doesn't appear to be any way to get
sComments1-1
from"sC" + (y + 1) + "Y" + (i + 1) + ""
so I'm not sure why you think your query string is of the correct form.In any case, your column names need to be surrounded by backticks rather than single quotes:
Assuming that
sComments1-1
is a typo or earlier version, and should actually be one of multiple columns of the formsCaYb
, wherea
andb
are distinct integers, the code would look something like this:I've added the backticks around the column name and removed them from the
sNumber
value (since it's an integer rather than a character column).您是否尝试过删除字段名称周围的单引号?不确定这是否有效,但这是我会尝试的一件事。
如果这没有帮助,请告诉我,我将删除此答案
Have you tried removing the single quotes around your field names? unsure this will work but it's one thing i'd try.
if this doesn't help, let me know and i'll remove this answer
列名“sComments1-1”不应作为带引号的字符串,只需编写不带引号的列名称即可。
The Columnname 'sComments1-1' shouldnt be as a quoted string, just write the column name without the Quotes.