In actual operation, the demand of the serial number is very big. For example, we often hope that the order number or the order number of products in order to automatically increment the sequential numbers without the need for manual input, when you encounter such requirements, do not want to be impulsive and to solve the program code, because as long as the use of automatic Number field, all the way!
Ado. NET does not provide additional, so-called AutoNumber data types. To make a field an AutoNumber field, you must make the following settings:
First, the data type of the field must be Int16, Int32, or Int64. If the data type of the field is not these three, the DataType property is enforced to Int32.
To make a field an AutoNumber field, you must set its AutoIncrement property to True.
AutoNumber fields are actually integers, except that whenever you add a new row of data, ADO. NET automatically increments the contents of the AutoNumber field of the last data row and fills the incremented result into the AutoNumber field of the new data row, which does not need to be entered by you.
However, as the ado.net automatically increments and fills in for you, you must use the AutoIncrementSeed property to set the seed (seed) of the AutoNumber field and use the AutoIncrementStep property to set the increment value of the AutoNumber field ( Increment) in order to allow ado.net to follow. The so-called "seed" is the first to be added to the data table data row of the AutoNumber field content. As for "increment value", it is the number of automatically numbered fields that are fixed each time from the previous data row. The default value for the seed is 0, and the default value of the recursive increment is 1.
For example, if you set the seed and increment value to 1, the AutoNumber field for each data row starting with the first pen will be 1, 2, 3, 4 ... For example, if you set the seed and increment values to 1 and 3 respectively, the AutoNumber field contents of each data row starting with the first pen will be 1, 4, 7, 10 ...
Because you do not need to enter data in the AutoNumber field yourself, it is recommended that you set the ReadOnly property of the AutoNumber field to true.
Program examples
Figure 5-8