Permalink
Fetching contributors…
Cannot retrieve contributors at this time
46 lines (40 sloc) 2.04 KB
title ms.custom ms.date ms.prod ms.reviewer ms.suite ms.technology ms.tgt_pltfrm ms.topic ms.assetid caps.latest.revision author ms.author manager
Creating AutoIncrement Columns
03/30/2017
.net-framework
dotnet-ado
article
cf09732a-ab54-4d98-89e2-4d0a1f28fbce
4
JennieHubbard
jhubbard
jhubbard

Creating AutoIncrement Columns

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 xref:System.Data.DataColumn, set the xref:System.Data.DataColumn.AutoIncrement%2A property of the column to true. The xref:System.Data.DataColumn then starts with the value defined in the xref:System.Data.DataColumn.AutoIncrementSeed%2A property, and with each row added the value of the AutoIncrement column increases by the value defined in the xref:System.Data.DataColumn.AutoIncrementStep%2A property of the column.

For AutoIncrement columns, we recommend that the xref:System.Data.DataColumn.ReadOnly%2A 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.

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

See Also

xref:System.Data.DataColumn
DataTable Schema Definition
DataTables
ADO.NET Managed Providers and DataSet Developer Center