DataColumn.AutoIncrement 属性


获取或设置一个值,该值指示对于添加到该表中的新行,列是否将列的值自动递增。Gets or sets a value that indicates whether the column automatically increments the value of the column for new rows added to the table.

 property bool AutoIncrement { bool get(); void set(bool value); };
public bool AutoIncrement { get; set; }
public bool AutoIncrement { get; set; }
member this.AutoIncrement : bool with get, set
member this.AutoIncrement : bool with get, set
Public Property AutoIncrement As Boolean



如果列的值自动递增,则为 true;否则为 falsetrue if the value of the column increments automatically; otherwise, false. 默认值为 falseThe default is false.



该列是计算所得的列。The column is a computed column.


下面的示例设置 AutoIncrementAutoIncrementSeedAutoIncrementStep 属性。The following example sets the AutoIncrement, AutoIncrementSeed, and AutoIncrementStep properties.

private void AddAutoIncrementColumn()
    DataColumn column = new DataColumn();
    column.DataType = System.Type.GetType("System.Int32");
    column.AutoIncrement = true;
    column.AutoIncrementSeed = 1000;
    column.AutoIncrementStep = 10;

    // Add the column to a new DataTable.
    DataTable table = new DataTable("table");
Private Sub AddAutoIncrementColumn()
    Dim column As New DataColumn()
    column.DataType = System.Type.GetType("System.Int32")
    With column
        .AutoIncrement = True
        .AutoIncrementSeed = 1000
        .AutoIncrementStep = 10
    End With

    ' Add the column to a new DataTable.
    Dim table As DataTable
    table = New DataTable
End Sub


如果在设置此属性时此列的类型不是 Int16、Int32 或 Int64,则将 DataType 属性强制转换为 Int32。If the type of this column is not Int16, Int32, or Int64 when this property is set, the DataType property is coerced to Int32. 如果这是一个计算列,则会生成异常。An exception is generated if this is a computed column. Expression设置属性。The Expression property is set. 仅当将此列的值(添加到列集合时)等于默认值时,才使用递增的值。The incremented value is used only if the row's value for this column, when added to the columns collection, is equal to the default value.

您可以使用类的属性创建一个新行 ItemArray DataRow ,并传入一个值数组。You can create a new row using the ItemArray property of the DataRow class and passing in an array of values. 对于设置为的列,这是一个潜在的问题 AutoIncrement true ,因为它的值是自动生成的。This is a potential problem for a column with its AutoIncrement set to true, because its value is generated automatically. 若要使用 ItemArray 属性,请将 null 列位置置于数组中的位置。To use the ItemArray property, place null in the column's position in the array. 有关详细信息,请参阅 ItemArray 类的属性 DataRowFor more information, see the ItemArray property of the DataRow class.

如果列的类型为 SqlInt16SqlInt32AutoIncrement 则将不起作用。If the type of the column is SqlInt16 or SqlInt32, AutoIncrement will not work. 请改用 Int16 或 Int32。Use Int16 or Int32 instead.

如果列的类型为 SqlInt64SqlDecimalAutoIncrement 将仅部分工作。If the type of the column is SqlInt64 or SqlDecimal, AutoIncrement will only partially work. 请改用 Int64 或 Decimal。Use Int64 or Decimal instead.

AutoIncrementStep 该值添加到当前值后,溢出检查将被取消。When the AutoIncrementStep value is added to the current value, the overflow check is suppressed.