表示内存中数据的一个表。
有关此类型所有成员的列表,请参阅 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 Serializable class DataTable extends MarshalByValueComponent implements IListSource, ISupportInitialize, ISerializable
线程安全
该类型对于多线程读操作是安全的。您必须使任何写操作同步。
备注
DataTable 是 ADO.NET 库中的核心对象。其他使用 DataTable 的对象包括 DataSet 和 DataView。
当访问 DataTable 对象时,注意它们是按条件区分大小写的。例如,如果一个 DataTable 被命名为“mydatatable”,另一个被命名为“Mydatatable”,则用于搜索其中一个表的字符串被认为是区分大小写的。但是,如果“mydatatable”存在而“Mydatatable”不存在,则认为该搜索字符串不区分大小写。有关使用 DataTable 对象的更多信息,请参见 tabindex="0" keywords="cpconCreatingDataTable">创建数据表。
如果正在以编程方式创建 DataTable,则必须先通过将 DataColumn 对象添加到 DataColumnCollection(通过 Columns 属性访问)中来定义其架构。有关添加 DataColumn 对象的更多信息,请参见 tabindex="0" keywords="cpconaddingdatacolumnstodatatable">在表中添加列。
若要向 DataTable 中添加行,必须先使用 NewRow 方法返回新的 DataRow 对象。NewRow 方法返回具有 DataTable 的架构的行,就像由该表的 DataColumnCollection 定义的那样。DataTable 可存储的最大行数是 16,777,216。有关更多信息,请参见 tabindex="0" keywords="cpconaddingdatatodatatable">将数据添至表中。
表的架构由 DataColumn 对象的集合 DataColumnCollection 定义。通过 Columns 属性访问 DataColumnCollection。有关定义表的架构的更多信息,请参见 DataColumn 和 DataColumnCollection。
DataTable 包含可用于确保数据完整性的 Constraint 对象的集合。有关更多信息,请参见 tabindex="0" keywords="cpconAddingConstraintsToDataSet">将约束添加到表。
若要确定何时对表进行更改,请使用以下某个事件:RowChanged、RowChanging、RowDeleting 和 RowDeleted。有关更多信息,请参见 tabindex="0" keywords="cpconWorkingWithDataTableEvents">使用 DataTable 事件。
当创建 DataTable 的实例时,某些读/写属性将被设置为初始值。有关这些值的列表,请参见 DataTable 构造函数。
注意 DataSet 和 DataTable 对象从 MarshalByValueComponent 继承而来,并支持用于远程处理的 ISerializable 接口。这些是仅有的可以远程处理的 ADO.NET 对象。
示例
[Visual Basic, C#, C++] 以下示例创建两个 DataTable 对象和一个 DataRelation 对象,并将这些新对象添加到 DataSet 中。然后通过调用 DataGrid.SetDataBinding 方法在 DataGrid 控件中显示这些表。
[Visual Basic] ' Put the next line into the Declarations section. private myDataSet As DataSet Private Sub MakeDataTables() ' Run all of the functions. MakeParentTable() MakeChildTable() MakeDataRelation() BindToDataGrid() End Sub Private Sub MakeParentTable() ' Create a new DataTable. Dim myDataTable As DataTable = new DataTable("ParentTable") ' Declare variables for DataColumn and DataRow objects. Dim myDataColumn As DataColumn Dim myDataRow As DataRow ' 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. Dim PrimaryKeyColumns(0) As DataColumn 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 Dim i As Integer For i = 0 to 2 myDataRow = myDataTable.NewRow() myDataRow("id") = i myDataRow("ParentItem") = "ParentItem " + i.ToString() myDataTable.Rows.Add(myDataRow) Next i End Sub Private Sub MakeChildTable() ' Create a new DataTable. Dim myDataTable As DataTable = New DataTable("childTable") Dim myDataColumn As DataColumn Dim myDataRow As DataRow ' 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. Dim i As Integer For i = 0 to 4 myDataRow = myDataTable.NewRow() myDataRow("childID") = i myDataRow("ChildItem") = "Item " + i.ToString() myDataRow("ParentID") = 0 myDataTable.Rows.Add(myDataRow) Next i For i = 0 to 4 myDataRow = myDataTable.NewRow() myDataRow("childID") = i + 5 myDataRow("ChildItem") = "Item " + i.ToString() myDataRow("ParentID") = 1 myDataTable.Rows.Add(myDataRow) Next i For i = 0 to 4 myDataRow = myDataTable.NewRow() myDataRow("childID") = i + 10 myDataRow("ChildItem") = "Item " + i.ToString() myDataRow("ParentID") = 2 myDataTable.Rows.Add(myDataRow) Next i End Sub Private Sub MakeDataRelation() ' DataRelation requires two DataColumn (parent and child) and a name. Dim myDataRelation As DataRelation Dim parentColumn As DataColumn Dim childColumn As DataColumn parentColumn = myDataSet.Tables("ParentTable").Columns("id") childColumn = myDataSet.Tables("ChildTable").Columns("ParentID") myDataRelation = new DataRelation("parent2Child", parentColumn, childColumn) myDataSet.Tables("ChildTable").ParentRelations.Add(myDataRelation) End Sub Private Sub BindToDataGrid() ' Instruct the DataGrid to bind to the DataSet, with the ' ParentTable as the topmost DataTable. DataGrid1.SetDataBinding(myDataSet,"ParentTable") End Sub [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"); } [C++] // Put the next line into the Declarations section. private: System::Data::DataSet* myDataSet; void MakeDataTables(){ // Run all of the functions. MakeParentTable(); MakeChildTable(); MakeDataRelation(); BindToDataGrid(); } void MakeParentTable(){ // Create a new DataTable. System::Data::DataTable* myDataTable = new DataTable(S"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(S"System.Int32"); myDataColumn->ColumnName = S"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(S"System.String"); myDataColumn->ColumnName = S"ParentItem"; myDataColumn->AutoIncrement = false; myDataColumn->Caption = S"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->Item[0] = myDataTable->Columns->Item[S"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->Item[S"id"] = __box(i); myDataRow->Item[S"ParentItem"] = String::Format( S"ParentItem {0}", __box(i)); myDataTable->Rows->Add(myDataRow); } } void MakeChildTable(){ // Create a new DataTable. DataTable* myDataTable = new DataTable(S"childTable"); DataColumn* myDataColumn; DataRow* myDataRow; // Create first column and add to the DataTable. myDataColumn = new DataColumn(); myDataColumn->DataType= System::Type::GetType(S"System.Int32"); myDataColumn->ColumnName = S"ChildID"; myDataColumn->AutoIncrement = true; myDataColumn->Caption = S"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(S"System.String"); myDataColumn->ColumnName = S"ChildItem"; myDataColumn->AutoIncrement = false; myDataColumn->Caption = S"ChildItem"; myDataColumn->ReadOnly = false; myDataColumn->Unique = false; myDataTable->Columns->Add(myDataColumn); // Create third column. myDataColumn = new DataColumn(); myDataColumn->DataType= System::Type::GetType(S"System.Int32"); myDataColumn->ColumnName = S"ParentID"; myDataColumn->AutoIncrement = false; myDataColumn->Caption = S"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->Item[S"childID"] = __box(i); myDataRow->Item[S"ChildItem"] = String::Format( S"Item {0}", __box(i)); myDataRow->Item[S"ParentID"] = __box(0); myDataTable->Rows->Add(myDataRow); } for(int i = 0; i <= 4; i ++){ myDataRow = myDataTable->NewRow(); myDataRow->Item[S"childID"] = __box(i + 5); myDataRow->Item[S"ChildItem"] = String::Format( S"Item {0}", __box(i)); myDataRow->Item[S"ParentID"] = __box(1); myDataTable->Rows->Add(myDataRow); } for(int i = 0; i <= 4; i ++){ myDataRow = myDataTable->NewRow(); myDataRow->Item[S"childID"] = __box(i + 10); myDataRow->Item[S"ChildItem"] = String::Format( S"Item {0}", __box(i)); myDataRow->Item[S"ParentID"] = __box(2); myDataTable->Rows->Add(myDataRow); } } void MakeDataRelation(){ // DataRelation requires two DataColumn (parent and child) and a name. DataRelation* myDataRelation; DataColumn* parentColumn; DataColumn* childColumn; parentColumn = myDataSet->Tables->Item[S"ParentTable"]->Columns->Item[S"id"]; childColumn = myDataSet->Tables->Item[S"ChildTable"]->Columns->Item[S"ParentID"]; myDataRelation = new DataRelation(S"parent2Child", parentColumn, childColumn); myDataSet->Tables->Item[S"ChildTable"]->ParentRelations->Add(myDataRelation); } void BindToDataGrid(){ // Instruct the DataGrid to bind to the DataSet, with the // ParentTable as the topmost DataTable. dataGrid1->SetDataBinding(myDataSet,S"ParentTable"); }
[JScript] 没有可用于 JScript 的示例。若要查看 Visual Basic、C# 或 C++ 示例,请单击页左上角的“语言筛选器”按钮 。
要求
命名空间: System.Data
平台: Windows 98, Windows NT 4.0, Windows ME, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 系列, .NET Framework 精简版
程序集: System.Data (在 System.Data.dll 中)
请参见
DataTable 成员 | System.Data 命名空间 | DataColumnCollection | DataColumn | DataRow | DataSet | DataView | DataRowCollection