複製鏈接
請複製以下鏈接發送給好友

DataColumn

鎖定
DataColumn 是用於創建 DataTable 的架構的基本構造塊。通過向 DataColumnCollection 中添加一個或多個 DataColumn 對象來生成這個架構。
中文名
DataColumn
屬    性
DataType
用    途
創建DataTable的架構
使用方法
創建聚合列

目錄

DataColumn使用方法

每個 DataColumn 都有 DataType 屬性,該屬性確定 DataColumn 所包含的數據的種類。例如,可以將數據類型限制為整數、字符串或小數。由於 DataTable 所包含的數據通常合併回其原始數據源,因此必須使其數據類型與數據源中的數據類型匹配。有關更多信息,請參見 將數據提供程序數據類型映射到 .NET Framework 數據類型。AllowDBNull、Unique 和 ReadOnly 等屬性對數據的輸入和更新施加限制,從而有助於確保數據完整性。還可以使用 AutoIncrement、AutoIncrementSeed 和 utoIncrementStep 屬性來控制數據自動生成。
可以通過創建一個 UniqueConstraint 並將其添加到 DataColumn 所屬的 DataTable 的 ConstraintCollection 中,來確保 DataColumn 中的值是唯一的。
若要創建 DataColumn 對象之間的關係,請創建 DataRelation 對象並將其添加到 DataSet 的 DataRelationCollection。可以使用 DataColumn 對象的 Expression 屬性來計算列中的值或創建聚合列

DataColumn示例

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