In ADO. net, three attributes of datacolumn are used to support automatic incremental columns: autoincrement, autoincrementseed, and autoincrementstep. You only need to set the autoincrement of datacolumn to true to generate an automatic increment value for the new row of the datatable. Let's look at an example:
Dataset DS = new dataset ();
Datatable dt = Ds. Tables. Add ("orders ");
Datacolumn Col = DT. Columns. Add ("orderid", typeof (INT ));
Col. autoincrement = true;
Col. autoincrementseed =-1;
Col. autoincrementstep =-1;
Col. readonly = true;
The above orderid column is set to auto increment. Note that the values of the next two statements are set to-1, which has some reason. Autoincrementseed and autoincrementstep control how to generate new values. When an empty table is encountered, ADO. Net assigns the value stored in autoincrementseed to the auto increment column of the first row, and then autoincrementstep generates subsequent auto increment values.
Cause: ADO. the auto-increment value generated in. NET is just a placeholder. A real new value is generated in the database. The displayed value is the auto-increment value of the new row that is not submitted to the database, the database may generate different values based on the generated values. Both autoincrementseed and autoincrementstep are set to-1, which ensures that the generated placeholder value does not appear in the database.
Therefore, when using autoincrement, set both autoincrementseed and autoincrementstep to-1.