Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Export (0) Print
Expand All
Important This document may not represent best practices for current development, links to downloads and other resources may no longer be valid. Current recommended version can be found here.

Creating AutoIncrement Columns (ADO.NET)

To ensure unique column values, you can set the column values to increment automatically when new rows are added to the table. To create an auto-incrementing DataColumn, set the AutoIncrement property of the column to true. The DataColumn then starts with the value defined in the AutoIncrementSeed property, and with each row added the value of the AutoIncrement column increases by the value defined in the AutoIncrementStep property of the column.

For AutoIncrement columns, we recommend that the ReadOnly property of the DataColumn be set to true.

The following example demonstrates how to create a column that starts with a value of 200 and adds incrementally in steps of 3.

DataColumn workColumn = workTable.Columns.Add(
    "CustomerID", typeof(Int32));
workColumn.AutoIncrement = true;
workColumn.AutoIncrementSeed = 200;
workColumn.AutoIncrementStep = 3;

Community Additions

ADD
Show:
© 2015 Microsoft