表示記憶體中資料的一個表。
有關此類型所有成員的列表,請參閱 DataTable 成員。
System.Object
System.ComponentModel.MarshalByValueComponent
System.Data.DataTable
[Visual Basic]<Serializable>Public Class DataTable Inherits MarshalByValueComponent Implements IListSource, ISupportInitialize, ISerializable[C#][Serializable]public class DataTable : MarshalByValueComponent, IListSource, ISupportInitialize, ISerializable[C++][Serializable]public __gc class DataTable : public MarshalByValueComponent, IListSource, ISupportInitialize, ISerializable[JScript]public Serializableclass DataTable extends MarshalByValueComponent implements IListSource, ISupportInitialize, ISerializable
安全執行緒
該類型對於多線程讀操作是安全的。您必須使任何寫操作同步。
備忘
DataTable 是 ADO.NET 庫中的核心對象。其他使用 DataTable 的對象包括 DataSet 和 DataView。
當訪問 DataTable 對象時,注意它們是按條件區分大小寫。例如,如果一個 DataTable 被命名為“mydatatable”,另一個被命名為“Mydatatable”,則用於搜尋其中一個表的字串被認為是區分大小寫。但是,如果“mydatatable”存在而“Mydatatable”不存在,則認為該搜尋字串不區分大小寫。有關使用 DataTable 對象的更多資訊,請參見。
如果正在以編程方式建立 DataTable,則必須先通過將 DataColumn 對象添加到 DataColumnCollection(通過 Columns 屬性訪問)中來定義其架構。有關添加 DataColumn 對象的更多資訊,請參見。
若要向 DataTable 中添加行,必須先使用 NewRow 方法返回新的 DataRow 對象。NewRow 方法返回具有 DataTable 的架構的行,就像由該表的 DataColumnCollection 定義的那樣。DataTable 可儲存的最大行數是 16,777,216。有關更多資訊,請參見。
表的架構由 DataColumn 對象的集合 DataColumnCollection 定義。通過 Columns 屬性訪問 DataColumnCollection。有關定義表的架構的更多資訊,請參見 DataColumn 和 DataColumnCollection。
DataTable 包含可用於確保資料完整性的 Constraint 對象的集合。有關更多資訊,請參見。
若要確定何時對錶變更,請使用以下某個事件:RowChanged、RowChanging、RowDeleting 和 RowDeleted。有關更多資訊,請參見。
當建立 DataTable 的執行個體時,某些讀/寫屬性將被設定為初始值。有關這些值的列表,請參見 DataTable 建構函式。
注意 DataSet 和 DataTable 對象從 MarshalByValueComponent 繼承而來,並支援用於遠端的 ISerializable 介面。這些是僅有的可以遠端的 ADO.NET 對象。
樣本
[C#]
// Put the next line into the Declarations section.
private System.Data.DataSet myDataSet;
private void MakeDataTables(){
// Run all of the functions.
MakeParentTable();
MakeChildTable();
MakeDataRelation();
BindToDataGrid();
}
private void MakeParentTable(){
// Create a new DataTable.
System.Data.DataTable myDataTable = new DataTable("ParentTable");
// Declare variables for DataColumn and DataRow objects.
DataColumn myDataColumn;
DataRow myDataRow;
// Create new DataColumn, set DataType, ColumnName and add to DataTable.
myDataColumn = new DataColumn();
myDataColumn.DataType = System.Type.GetType("System.Int32");
myDataColumn.ColumnName = "id";
myDataColumn.ReadOnly = true;
myDataColumn.Unique = true;
// Add the Column to the DataColumnCollection.
myDataTable.Columns.Add(myDataColumn);
// Create second column.
myDataColumn = new DataColumn();
myDataColumn.DataType = System.Type.GetType("System.String");
myDataColumn.ColumnName = "ParentItem";
myDataColumn.AutoIncrement = false;
myDataColumn.Caption = "ParentItem";
myDataColumn.ReadOnly = false;
myDataColumn.Unique = false;
// Add the column to the table.
myDataTable.Columns.Add(myDataColumn);
// Make the ID column the primary key column.
DataColumn[] PrimaryKeyColumns = new DataColumn[1];
PrimaryKeyColumns[0] = myDataTable.Columns["id"];
myDataTable.PrimaryKey = PrimaryKeyColumns;
// Instantiate the DataSet variable.
myDataSet = new DataSet();
// Add the new DataTable to the DataSet.
myDataSet.Tables.Add(myDataTable);
// Create three new DataRow objects and add them to the DataTable
for (int i = 0; i<= 2; i++){
myDataRow = myDataTable.NewRow();
myDataRow["id"] = i;
myDataRow["ParentItem"] = "ParentItem " + i;
myDataTable.Rows.Add(myDataRow);
}
}
private void MakeChildTable(){
// Create a new DataTable.
DataTable myDataTable = new DataTable("childTable");
DataColumn myDataColumn;
DataRow myDataRow;
// Create first column and add to the DataTable.
myDataColumn = new DataColumn();
myDataColumn.DataType= System.Type.GetType("System.Int32");
myDataColumn.ColumnName = "ChildID";
myDataColumn.AutoIncrement = true;
myDataColumn.Caption = "ID";
myDataColumn.ReadOnly = true;
myDataColumn.Unique = true;
// Add the column to the DataColumnCollection.
myDataTable.Columns.Add(myDataColumn);
// Create second column.
myDataColumn = new DataColumn();
myDataColumn.DataType= System.Type.GetType("System.String");
myDataColumn.ColumnName = "ChildItem";
myDataColumn.AutoIncrement = false;
myDataColumn.Caption = "ChildItem";
myDataColumn.ReadOnly = false;
myDataColumn.Unique = false;
myDataTable.Columns.Add(myDataColumn);
// Create third column.
myDataColumn = new DataColumn();
myDataColumn.DataType= System.Type.GetType("System.Int32");
myDataColumn.ColumnName = "ParentID";
myDataColumn.AutoIncrement = false;
myDataColumn.Caption = "ParentID";
myDataColumn.ReadOnly = false;
myDataColumn.Unique = false;
myDataTable.Columns.Add(myDataColumn);
myDataSet.Tables.Add(myDataTable);
// Create three sets of DataRow objects, five rows each, and add to DataTable.
for(int i = 0; i <= 4; i ++){
myDataRow = myDataTable.NewRow();
myDataRow["childID"] = i;
myDataRow["ChildItem"] = "Item " + i;
myDataRow["ParentID"] = 0 ;
myDataTable.Rows.Add(myDataRow);
}
for(int i = 0; i <= 4; i ++){
myDataRow = myDataTable.NewRow();
myDataRow["childID"] = i + 5;
myDataRow["ChildItem"] = "Item " + i;
myDataRow["ParentID"] = 1 ;
myDataTable.Rows.Add(myDataRow);
}
for(int i = 0; i <= 4; i ++){
myDataRow = myDataTable.NewRow();
myDataRow["childID"] = i + 10;
myDataRow["ChildItem"] = "Item " + i;
myDataRow["ParentID"] = 2 ;
myDataTable.Rows.Add(myDataRow);
}
}
private void MakeDataRelation(){
// DataRelation requires two DataColumn (parent and child) and a name.
DataRelation myDataRelation;
DataColumn parentColumn;
DataColumn childColumn;
parentColumn = myDataSet.Tables["ParentTable"].Columns["id"];
childColumn = myDataSet.Tables["ChildTable"].Columns["ParentID"];
myDataRelation = new DataRelation("parent2Child", parentColumn, childColumn);
myDataSet.Tables["ChildTable"].ParentRelations.Add(myDataRelation);
}
private void BindToDataGrid(){
// Instruct the DataGrid to bind to the DataSet, with the
// ParentTable as the topmost DataTable.
dataGrid1.SetDataBinding(myDataSet,"ParentTable");
}