DataTable class

xiaoxiao2021-03-06  20

Indicates a table of data in memory.

For a list of all members of this type, see the DataTable member.

System.Object System.componentmodel.MarshalByValueComponent System.Data.DataTable

[Visual Basic]

Public Class DataTable

Inherits MarshalByValueComponent

Implements ILISTSOURCE, ISUPPORTINIALIZE, ISERIALIZABLE

[C #]

[Serializable]

Public Class DataTable: MarshalByValueComponent, ILISTSOURCE,

ISupportInitialize, iSerializable

[C ]

[Serializable]

Public __gc class dataatable: Public MarshalByValueComponent,

Ilistsource, IsupportInitialize, iSerializable

[Jscript]

public

Serializable

Class DataTable Extends MarshalByValueComponent Implements

Ilistsource, IsupportInitialize, iSerializable

Thread security

This type is safe for multi-threaded readings. You must synchronize any write operation.

Note

DataTable is the core object in the ADO.NET library. Other objects using DataTable include DataSet and DataView.

When accessing the DataTable object, pay attention to them is case sensitive. For example, if a DataTable is named "MyDataTable", the other is named "MyDataTable", the string used to search for one of the tables is considered to be case sensitive. However, if "MyDataTable" exists, "myDataTable" does not exist, it is considered that the search string is not case sensitive. For more information on using the DataTable object, see TabINDEX = "0" Keywords = "CPConcReatingDataTable"> Create a data sheet.

If you are creating a DataTable in a programming, you must first define its architecture by adding a DataColumn object to the DatacolumnCollection (via Column Attribute Access). For more information on adding a DataColumn object, see TabINDEX = "0" Keywords = "cpconaddingDatacolumntodataable"> Add columns in the table.

To add rows to DataTable, you must first return to the new DataRow object using the new path method. The Newrow method returns a line with the DataTable schema, just like the DatacolumnCollection definition of the table. The maximum number of lines that DataBest can be stored is 16,777,216. For more information, see TabINDEX = "0" Keywords = "cpconaddingDataTatodATABLE"> Add the data to the table.

The schema of the table is defined by the collection of Datacolumn objects. Access DatacolumnCollection via the Columns property. For more information on the architecture of the definition table, see Datacolumn and DatacolumnCollection. DataTable contains a collection of constraint objects that can be used to ensure data integrity. For more information, see TabINDEX = "0" Keywords = "CPConAddingConstRAINTSTODATASET"> Add the constraint to the table.

To make sure you change the table, use one of the following events: RowChanged, Rowchanging, Rowdeleting and Rowdeleted. For more information, see TabINDEX = "0" Keywords = "CPConworkingWithDataTableEvents"> Using the DataTable event.

When you create a DataTable instance, some read / write properties will be set to the initial value. For a list of these values, see the DataTable constructor.

note

DataSet and

DataTable object

MarshalByValueComponent inherits and supports remote processing

ISerializable interface. These are the only ADO.NET objects that can be processed remotely.

Example

[Visual Basic, C #, C ] The following example creates two DataTable objects and a DataRelation object and adds these new objects to the DataSet. These tables are then displayed in the DataGrid control by calling the DataGrid.SetDataBinding method.

[Visual Basic]

'Put the next line into the declarations section.

Private mydataset as dataset

Private sub makedataables ()

'Run All of the functions.

MakeparentTable ()

MakechildTable ()

Makedataralation ()

BindtodataGrid ()

End Sub

Private sub makeparenttable ()

'Create a new dataable.

Dim myDataTable as datatable = new data ("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 colorn 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 colorn 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 DataBole

DIM I as integer

For i = 0 TO 2

MyDATAROW = myDataBLE.NEWROW ()

MyDATAROW ("ID") = i

MyDATAROW ("ParentItem") = "ParentItem" i.toString ()

MyDataTable.Rows.Add (MyDataRow)

Next I

End Sub

Private subechildtable ()

'Create a new dataable.

DIM myDataTable as datatable = new data ("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 colorn 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 = myDataBLE.NEWROW ()

MyDATAROW ("ChildID") = i

MyDATAROW ("ChildItem") = "item" i.tostring ()

MyDATAROW ("ParentID") = 0

MyDataTable.Rows.Add (MyDataRow)

Next I

For i = 0 TO 4

MyDATAROW = myDataBLE.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 = myDataBLE.NEWROW ()

MyDATAROW ("ChildID") = i 10

MyDATAROW ("ChildItem") = "item" i.tostring ()

MyDATAROW ("ParentID") = 2

MyDataTable.Rows.Add (MyDataRow)

Next I

End Sub

Private sub makedataralation ()

'DataRelation Requires Two Datacolumn (Parent and Child) and a name.dim mydataralation as dataralation

DIM PARENTCOLUMN AS Datacolumn

Dim ChildColumn As Datacolumn

ParentColumn = mydataset.tables ("ParentTable"). Column ("ID")

ChildColumn = mydataset.tables ("childtable"). Column ("parentid")

MyDataralation = New DataRelation ("Parent2child", ParentColumn, ChildColumn)

MyDataSet.Tables ("ChildTable"). ParentRelations.Add (MyDataLelation)

End Sub

Private sub bindtodatagrid ()

'Instruct the datagrid to bind to the dataset, with the

'ParentTable as the topmost dataable.

DataGrid1.SetDataBinding (MyDataSet, "ParentTable")

End Sub

[C #]

// put the next line into the declarations section.

Private system.data.dataset mydataset;

Private void makeData tables () {

// Run all of the functions.

MakeparentTable ();

MakechildTable ();

Makedataralation ();

BindtodataGrid ();

}

Private void makeparenttable () {

// CREATE A New DataTable.

System.Data.DataTable mydataable = new dataable ("parentTable");

// Declare Variables for Datacolumn and DataRow Objects.

Datacolumn mydatacolumn;

DataRow MyDataRow;

// Create New Datacolumn, Set DataType, ColumnName and Add to DataBole.

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] = mydataable.columns ["id"];

MyDataTable.primaryKey = primaryKeyColumn;

// Instantiate The Dataset Variable.

MyDataSet = new dataset ();

// add the new dataatable 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 = myDataBLE.NEWROW ();

MyDataRow ["ID"] = i;

MyDataRow ["ParentItem"] = "ParentItem" i;

MyDataTable.Rows.Add (MyDATAROW);

}

}

Private void makechildtable () {

// CREATE A New DataTable.

DataTable mydataable = new dataable ("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 = "child"; 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 = myDataBLE.NEWROW ();

MyDataRow ["ChildID"] = i;

MyDaTarow ["ChildItem"] = "Item" i;

MyDataRow ["ParentID"] = 0;

MyDataTable.Rows.Add (MyDATAROW);

}

For (int i = 0; i <= 4; i ) {

mydatarow = myDataBLE.NEWROW ();

MyDATAROW ["ChildID"] = i 5;

MyDaTarow ["ChildItem"] = "Item" i;

MyDaTarow ["ParentID"] = 1;

MyDataTable.Rows.Add (MyDATAROW);

}

For (int i = 0; i <= 4; i ) {

mydatarow = myDataBLE.NEWROW ();

MyDaTarow ["childid"] = i 10;

MyDaTarow ["ChildItem"] = "Item" i;

MyDATAROW ["ParentID"] = 2;

MyDataTable.Rows.Add (MyDATAROW);

}

}

Private void makedataralation () {

// DataRelation Requires Two Datacolumn (Parent and child) and a name.

Dataralation myDatarad;

Datacolumn parentcolumn;

Datacolumn ChildColumn; ParentColumn = MyDataSet.tables ["ParentTable"]. Columns ["ID"];

ChildColumn = mydataset.tables ["childtable"]. Columns ["parentid"];

MyDataralation = New DataRelation ("Parent2child", ParentColumn, ChildColumn);

MyDataSet.Tables ["childtable"]. ParentRelations.add (MyDataLalation);

}

Private void bindtodataGrid () {

// instruct the datagert to bind to the dataset, with the

// parenttable as the topmost dataable.

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 ();

Makedataralation ();

BindtodataGrid ();

}

Void makeparentTable () {

// CREATE A New DataTable.

System :: Data :: DataTable * mydataable = new data (s "ParentTable");

// Declare Variables for Datacolumn and DataRow Objects.

Datacolumn * mydatacolumn;

DataRow * mydatarow;

// Create New Datacolumn, Set DataType, ColumnName and Add to DataBole.

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.sw");

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 * primaryKeyColumn [] = new datacolumn * [1];

PRIMARYKEYCOLUMNS-> item [0] = mydataable-> columns-> item [s "id"];

MyDataTable-> PrimaryKey = primaryKeyColumn;

// Instantiate The Dataset Variable.

MyDataSet = new dataset ();

// add the new dataatable 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 = myDataBLE-> 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 data (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 = myDataBLE-> 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 = myDataBLE-> 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 = myDataBLE-> newrow ();

MyDATAROW-> item [s "childid"] = __box (i 10); mydatarow-> item [s "child"] = String :: format (s "item {0}", __box (i));

MyDATAROW-> Item [s "parentid"] = __box (2);

MyDataTable-> rows-> add (myDATAROW);

}

}

Void makeDataralation () {

// DataRelation Requires Two Datacolumn (Parent and child) and a name.

Dataralaization * mydataralation;

Datacolumn * parentcolumn;

Datacolumn * childColumn;

ParentColumn = myDataSet-> Tables-> item [s "parenttable"] -> columns-> item [s "id"];

ChildColumn = MyDataSet-> Tables-> item [s "childtable"] -> columns-> item [s "parentid"];

MyDataralation = New DataRelation (s "Parent2child", ParentColumn, ChildColumn;

MyDataSet-> Tables-> item [s "childtable"] -> ParentRelations-> add (myDataralation);

}

Void bindtodatagrid () {

// instruct the datagert to bind to the dataset, with the

// parenttable as the topmost dataable.

DataGrid1-> setDataBinding (MyDataSet, S "ParentTable");

}

[JScript] There is no example available for JScript. To view Visual Basic, C # or C examples, click the "Language Filter" button in the upper left corner.

Claim

Namespace: system.data

Platform: Windows 98, Windows NT 4.0, Windows ME, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 Series, .NET Framework Lite

Accessure: system.data (in System.Data.dll)

See

DataTable Member | System.Data Name Space | DatacolumnCollection | Datacolumn | DataRow | DataSet | DataView | DataRowCollection

Syntax document version 1.1.1 based on .NET Framework version 1.1.1

转载请注明原文地址:https://www.9cbs.com/read-42851.html

New Post(0)