概述
DataColumn 是用于建立 DataTable 的架构的基本构造块。通过向 DataColumnCollection 中增加一个或多个 DataColumn 对象来生成这个架构。
每个 DataColumn 都有 DataType 属性,该属性确定 DataColumn 所包含的资料的种类。例如,可以将资料类型限製为整数、字元串或小数。由于 DataTable 所包含的资料通常合并回其原始资料源,因此必须使其资料类型与资料源中的资料类型匹配。有关更多信息,请参见 将资料提供程式资料类型对应到 .NET Framework 资料类型。AllowDBNull、Unique 和 ReadOnly 等属性对资料的输入和更新施加限製,从而有助于确保资料完整性。还可以使用 AutoIncrement、AutoIncrementSeed 和 utoIncrementStep 属性来控製资料自动生成。
可以通建立一个 UniqueConstraint 并将其增加到 DataColumn 所属的 DataTable 的 ConstraintCollection 中,来确保 DataColumn 中的值是唯一的。
若要建立 DataColumn 对象之间的关系,请建立 DataRelation 对象并将其增加到 DataSet 的 DataRelationCollection。可以使用 DataColumn 对象的 Expression 属性来计算列中的值或建立聚合列
示例
Private Sub MakeTable()
' Create a DataTable.
Dim table As DataTable = new DataTable("Product")
' Create a DataColumn and set various properties.
Dim column As DataColumn = New DataColumn
column.DataType = System.Type.GetType("System.Decimal")
column.AllowDBNull = False
column.Caption = "Price"
column.ColumnName = "Price"
column.DefaultValue = 25
' Add the column to the table.
table.Columns.Add(column)
' Add 10 rows and set values.
Dim row As DataRow
Dim i As Integer
For i = 0 to 9
row = table.NewRow()
row("Price") = i + 1
' Be sure to add the new row to
' the DataRowCollection.
table.Rows.Add(row)
Next i
End Sub
















