0

当我按下插入时,我收到错误:Syntax error in INSERT INTO statement

  • EmployeeID(自动编号)
  • EmployeeName(文本)
  • Position(文本)
  • Address(文本)

    OleDbDataAdapter ad;
    DataSet ds;
    DataTable dt;
    
    protected void SetInsertParameters()
    {
        string sql = "INSERT INTO Employee(EmployeeName,Position,Address)"+
                     " VALUES (@EmployeeName,@Position,@Address)";
        ad.InsertCommand = new OleDbCommand(sql, con);
    
    
        OleDbParameter param = new OleDbParameter("@EmployeeName", OleDbType.VarChar);
        param.SourceColumn = "EmployeeName";
        param.SourceVersion = DataRowVersion.Current;
        ad.InsertCommand.Parameters.Add(param);
    
        param = new OleDbParameter("@Position", OleDbType.VarChar);
        param.SourceColumn = "Position";
        param.SourceVersion = DataRowVersion.Current;
        ad.InsertCommand.Parameters.Add(param);
    
        param = new OleDbParameter("@Address", OleDbType.VarChar);
        param.SourceColumn = "Address";
        param.SourceVersion = DataRowVersion.Current;
        ad.InsertCommand.Parameters.Add(param);
    }
    
    
    void InsertNewValues()
    {
        dt = ds.Tables["Employee"];
        DataRow row = dt.NewRow();
        row[0] = txt_employeeID.Text;
        row[1] = txt_name.Text;
        row[2] = txt_position.Text;
        row[3] = txt_address.Text;
        dt.Rows.Add(row);
        ad.Update(ds, "Employee");
        ad.Fill(ds);
    
    }
    
4

1 回答 1

0

Position 是保留字,必须用括号括起来:

 "INSERT INTO Employee(EmployeeName,[Position],Address)"
于 2013-01-01T22:58:01.877 回答