如何解决" INSERT INTO语句中的语法错误"

时间:2016-12-09 10:13:33

标签: c# ms-access oledb

连接正常。我只是在这部分面临问题。我的RECORD表的设计视图如下:

名称类型大小

     RecordID                Long Integer                             4
     BatchID                 Long Integer                             4

     ProcessID               Long Integer                             4
     EmployeeID              Long Integer                             4
     Start                   Date With Time                           8

     End                     Date With Time                           8
     Status                  Yes/No                                   1
     Hour                    Long Integer

这是我的代码:

protected void btnSave_Click(object sender, EventArgs e)
    {

        DateTime a = new DateTime(cldStart.SelectedDate.Year,cldStart.SelectedDate.Month,cldStart.SelectedDate.Day,Convert.ToInt32(ddlStartHour.SelectedValue),Convert.ToInt32(ddlStartMinute.SelectedValue),00);
        DateTime b = new DateTime(cldEnd.SelectedDate.Year, cldEnd.SelectedDate.Month, cldEnd.SelectedDate.Day, Convert.ToInt32(ddlEndHour.SelectedValue), Convert.ToInt32(ddlEndMinute.SelectedValue), 00);
        TimeSpan c = b-a;

        //insert into record table
        using (OleDbConnection con = new OleDbConnection(conString))
        {
            try
            {
                OleDbCommand cmd = new OleDbCommand();
                cmd.CommandType = CommandType.Text;
                cmd.CommandText = "INSERT INTO RECORD (BatchID,ProcessID,EmployeeID,Start,End,Status,Hour) VALUES (@batchid,@processid,@employeeid,@start,@end,@status,@hours)";
                cmd.Parameters.AddWithValue("@batchid", Convert.ToInt32(ddlBatchNo.SelectedValue));
                cmd.Parameters.AddWithValue("@processid", Convert.ToInt32(ddlSubCategory.SelectedValue));
                cmd.Parameters.AddWithValue("@employeeid", Convert.ToInt32(ddlEmployeeId.SelectedValue));
                   cmd.Parameters.AddWithValue("@start",a.ToString() );
                   cmd.Parameters.AddWithValue("@end", b.ToString());
                   cmd.Parameters.AddWithValue("@hours", c.ToString());
                   cmd.Parameters.AddWithValue("@status",chboxStatus.Checked);
                cmd.Connection = con;
                con.Open();
                cmd.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                txtRemark.Text = "wrong";
            }
        }

    }

我正在尝试插入记录表,但它显示了我

  

" InSERT into命令中的语法错误"。

不知道出了什么问题。

4 个答案:

答案 0 :(得分:4)

OleDbCommand没有名称参数的概念,因此您必须按照与查询相同的顺序定义参数。

   cmd.Parameters.AddWithValue("@hours", c.ToString());
   cmd.Parameters.AddWithValue("@status",chboxStatus.Checked);

所以你应该

   cmd.Parameters.AddWithValue("@status",chboxStatus.Checked);
   cmd.Parameters.AddWithValue("@hours", c.ToString());

但在您的查询中,状态是在数小时之前。还可以像其他人提到的那样修复你的保留字。

答案 1 :(得分:3)

您的列名称是end,hour之类的关键字,因此您需要将其括在大括号中

cmd.CommandText = "INSERT INTO RECORD (BatchID,ProcessID,EmployeeID,Start,[End],Status,[Hour]) VALUES (@batchid,@processid,@employeeid,@start,@end,@status,@hours)";

答案 2 :(得分:2)

Start,End,Status,Hour字段名称是关键字。将它们更改为[Start],[End],[Status],[Hour]或更改数据库

中的字段名称

答案 3 :(得分:0)

您的表名与记录关键字

相同

尝试用括号括起来

protected void btnSave_Click(object sender,EventArgs e)     {

    DateTime a = new DateTime(cldStart.SelectedDate.Year,cldStart.SelectedDate.Month,cldStart.SelectedDate.Day,Convert.ToInt32(ddlStartHour.SelectedValue),Convert.ToInt32(ddlStartMinute.SelectedValue),00);
    DateTime b = new DateTime(cldEnd.SelectedDate.Year, cldEnd.SelectedDate.Month, cldEnd.SelectedDate.Day, Convert.ToInt32(ddlEndHour.SelectedValue), Convert.ToInt32(ddlEndMinute.SelectedValue), 00);
    TimeSpan c = b-a;

    //insert into record table
    using (OleDbConnection con = new OleDbConnection(conString))
    {
        try
        {
            OleDbCommand cmd = new OleDbCommand();
            cmd.CommandType = CommandType.Text;
            cmd.CommandText = "INSERT INTO [RECORD] (BatchID,ProcessID,EmployeeID,Start,End,Status,Hour) VALUES (@batchid,@processid,@employeeid,@start,@end,@status,@hours)";
            cmd.Parameters.AddWithValue("@batchid", Convert.ToInt32(ddlBatchNo.SelectedValue));
            cmd.Parameters.AddWithValue("@processid", Convert.ToInt32(ddlSubCategory.SelectedValue));
            cmd.Parameters.AddWithValue("@employeeid", Convert.ToInt32(ddlEmployeeId.SelectedValue));
               cmd.Parameters.AddWithValue("@start",a.ToString() );
               cmd.Parameters.AddWithValue("@end", b.ToString());
               cmd.Parameters.AddWithValue("@hours", c.ToString());
               cmd.Parameters.AddWithValue("@status",chboxStatus.Checked);
            cmd.Connection = con;
            con.Open();
            cmd.ExecuteNonQuery();
        }
        catch (Exception ex)
        {
            txtRemark.Text = "wrong";
        }
    }

}