如何插入新值自动编号的参数访问



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

  • EmployeeID(自动编号)
  • EmployeeName (Text)
  • Position (Text)
  • Address (Text)

    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);
    }
    

Position为保留字,必须加括号:

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

相关内容

  • 没有找到相关文章

最新更新