如何通过参数插入新值autonumber来访问

时间:2013-01-01 21:21:43

标签: sql ms-access autonumber

当我按下插入时,收到错误: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);
    
    }
    

1 个答案:

答案 0 :(得分:0)

排名是reserved word,必须加上括号:

 "INSERT INTO Employee(EmployeeName,[Position],Address)"