简体   繁体   English

SQL语法错误C#

[英]SQL Syntax Error C#

I am fairly new to C#, and i am trying to make a teacher management program. 我对C#相当陌生,并且正在尝试制定一个教师管理程序。

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: 这是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 ,

Using MySQL 5.5 使用MySQL 5.5

I do not know why, but this is giving me sql errors. 我不知道为什么,但是这给了我SQL错误。 Please help me, my assignment is due in 2 days and i really need this finished. 请帮助我,我的作业要在2天之内完成,我真的需要完成此工作。

A duplicate of your problem (minus signs in column names) is asked and answered here: 在此询问并回答了您的问题的重复项(列名中的减号):

MySQL INSERT - Do field names require backtick/accent delimination? MySQL INSERT-字段名称是否需要反引号/重音符号?

You need to use 'back-ticks' instead on single quotes when using the column name with a minus sign in your query. 在查询中使用带有减号的列名时,需要在单引号上使用“反引号”。 Like this: 像这样:

UPDATE student SET `sComments1-1` = 'wa5235' WHERE sNumber = 68721919

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 如果这没有帮助,请告诉我,我将删除此答案

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. 似乎没有任何方法可以从"sC" + (y + 1) + "Y" + (i + 1) + "" sComments1-1 "sC" + (y + 1) + "Y" + (i + 1) + ""获取sComments1-1 ,因此我不确定为什么您认为查询字符串是正确的形式。

In any case, your column names need to be surrounded by backticks rather than single quotes: 无论如何,您的列名都需要用反引号而不是单引号引起来:

UPDATE student SET `sComments1-1` = ...

Assuming that sComments1-1 is a typo or earlier version, and should actually be one of multiple columns of the form sCaYb , where a and b are distinct integers, the code would look something like this: 假设sComments1-1是一个拼写错误或更早的版本,并且实际上应该是sCaYb形式的多个列之一,其中ab是不同的整数,代码看起来像这样:

// Get column name of form sC*Y* where *'s are distinct integers.

string commentColumn = "sC" + (y + 1) + "Y" + (i + 1);

// Execute query with correct quote types.

executeQuery("UPDATE student SET `" + commentColumn + "` = '" +
    s.getStudentCourses(i,y,s) +
    "' WHERE sNumber = " + s.getStudNumber(s));

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). 我在列名周围添加了反引号, 并将它们从sNumber值中删除 (因为它是整数而不是字符列)。

"UPDATE student SET 'sComments1-1' = 'wa5235' WHERE sN....

列名'sComments1-1'不应该作为带引号的字符串,只要写出不带引号的列名即可。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM