' - '2附近的语法不正确

时间:2016-04-04 15:06:08

标签: c#

我的C#项目有问题,我真的找不到问题......

调试说:

  

' - '

附近的语法不正确

这是有问题的代码:

 if (!MyAdoHelperCsharp.IsExist("DB.mdf", sql))
 {       
    insertQuery = "INSERT INTO Tables (TableNum,SitNum,Position,Busy,Sunday-7:00,Sunday-8:00,Sunday-9:00,Sunday-10:00,Sunday-11:00,Sunday-12:00,Sunday-13:00,Sunday-14:00,Sunday-15:00,Sunday-16:00,Sunday-17:00,Sunday-18:00,Sunday-19:00,Sunday-20:00,Sunday-21:00,Sunday-22:00,Monday-7:00,Monday-8:00,Monday-9:00,Monday-10:00,Monday-11:00,Monday-12:00,Monday-13:00,Monday-14:00,Monday-15:00,Monday-16:00,Monday-17:00,Monday-18:00,Monday-19:00,Monday-20:00,Monday-21:00,Monday-22:00,Tuesday-7:00,Tuesday-8:00,Tuesday-9:00,Tuesday-10:00,Tuesday-11:00,Tuesday-12:00,Tuesday-13:00,Tuesday-14:00,Tuesday-15:00,Tuesday-16:00,Tuesday-17:00,Tuesday-18:00,Tuesday-19:00,Tuesday-20:00,Tuesday-21:00,Tuesday-22:00,Wednesday-7:00,Wednesday-8:00,Wednesday-9:00,Wednesday-10:00,Wednesday-11:00,Wednesday-12:00,Wednesday-13:00,Wednesday-14:00,Wednesday-15:00,Wednesday-16:00,Wednesday-17:00,Wednesday-18:00,Wednesday-19:00,Wednesday-20:00,Wednesday-21:00,Wednesday-22:00,Thursday-7:00,Thursday-8:00,Thursday-9:00,Thursday-10:00,Thursday-11:00,Thursday-12:00,Thursday-13:00,Thursday-14:00,Thursday-15:00,Thursday-16:00,Thursday-17:00,Thursday-18:00,Thursday-19:00,Thursday-20:00,Thursday-21:00,Thursday-22:00,Friday-7:00,Friday-8:00,Friday-9:00,Friday-10:00,Friday-11:00,Friday-12:00,Friday-13:00,Friday-14:00) VALUES ('";
    insertQuery += tablenum + "','" + SitNum + "','" + position + "','" + false+ "','" + false+ "','" + false+ "','" + false+ "','" + false+ "','" + false+ "','" + false+ "','" + false+ "','" + false+ "','" + false+ "','" + false+ "','" + false+ "','" + false+ "','" + false+ "','" + false+ "','" + false+ "','" + false+ "','" + false+ "','" + false+ "','" + false+ "','" + false+ "','" + false+ "','" + false+ "','" + false+ "','" + false+ "','" + false+ "','" + false+ "','" + false+ "','" + false+ "','" + false+ "','" + false+ "','" + false+ "','" + false+ "','" + false+ "','" + false+ "','" + false+ "','" + false+ "','" + false+ "','" + false+ "','" + false+ "','" + false+ "','" + false+ "','" + false+ "','" + false+ "','" + false+ "','" + false+ "','" + false+ "','" + false+ "','" + false+ "','" + false+ "','" + false+ "','" + false+ "','" + false+ "','" + false+ "','" + false+ "','" + false+ "','" + false+ "','" + false+ "','" + false+ "','" + false+ "','" + false+ "','" + false+ "','" + false+ "','" + false+ "','" + false+ "','" + false+ "','" + false+ "','" + false+ "','" + false+ "','" + false+ "','" + false+ "','" + false+ "','" + false+ "','" + false+ "','" + false+ "','" + false+ "','" + false+ "','" + false+ "','" + false+ "','" + false+ "','" + false+ "','" + false+ "','" + false+ "','" + false+ "','" + false+ "','" + false+ "','" + false+ "','" + false+ "')";
    MessageBox.Show(insertQuery);
    MyAdoHelperCsharp.DoQuery("DB.mdf", insertQuery);
}

非常感谢!抱歉长码:)

1 个答案:

答案 0 :(得分:0)

问题是您的列名正在使用运算符,哪些SQL无法正确理解并且认为实际上是在尝试进行计算,在这种情况下是Sunday - 1:00,这显然不会给出答案,实际上是一个错误。这就是导致错误的原因

  

' - '

附近的语法不正确

要解决此问题,包含运算符,保留关键字或任何其他无效字符(例如空格)的任何字段,您需要将它们包含在方括号中,以便SQL 知道它是字段名称而不是猜测它然后弄错了,例如

insertQuery = "INSERT INTO Tables (TableNum, SitNum, Position,Busy, [Sunday-7:00], [Sunday-8:00],...)"

也可能值得一次又一次地将换行符放入代码中,因为这非常难以理解这有助于读取长字符串,例如SQL语句,如此

insertQuery = "INSERT INTO Tables 
(TableNum,SitNum,Position,Busy,
[Sunday-7:00],[Sunday-8:00],...)";