Mysql Delete语句被解释错误

时间:2015-04-15 18:20:48

标签: c# sql

这可能只是让我看错了,但我需要另一个人的眼睛来看它,因为我无法看到它。

为什么会这样:

comm.CommandText = "Delete from COURSE where COURSE_NAME='" + theCourse.CourseName() + "';"; // create query

在结尾处输出报价而不是'

结果:

Delete from COURSE where COURSE_NAME='LEARNING VITALS"

2 个答案:

答案 0 :(得分:2)

不要!在构建sql语句时连接字符串,这样就可以为sql注入攻击http://www.w3schools.com/sql/sql_injection.asp打开。改为使用参数。

comm.CommandText = "DELETE FROM Course WHERE Course_Name=@name";
comm.Parameters.AddWithValue("@name", "theNameOfTheCourse");

答案 1 :(得分:0)

好的,不要问我为什么或如何,但是当你放置&#34 ;;"从引号中'出现了"消失并且有效。

编辑代码:

comm.CommandText = "Delete from COURSE where COURSE_NAME='" + theCourse.CourseName() + "'; ";