Discussing automatic incremental columns in ADO. net

Source: Internet
Author: User

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.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.