建立自動遞增資料行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. 若要建立自動遞增DataColumn,請AutoIncrement將資料行的屬性設定為trueTo create an auto-incrementing DataColumn, set the AutoIncrement property of the column to true. 接著會以AutoIncrementSeed屬性中定義的值為開頭,而且每個資料列加上自動遞增資料行的值,都會隨著資料行AutoIncrementStep的屬性中所定義的值而增加。 DataColumnThe 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.

針對自動遞增資料行,我們建議ReadOnlyDataColumn的屬性設定為trueFor AutoIncrement columns, we recommend that the ReadOnly property of the DataColumn be set to true.

下列範例示範如何建立以 200 值做為開頭的資料行,並以增量 3 累加。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