DataTable クラス
アセンブリ: System.Data (system.data.dll 内)
構文
<SerializableAttribute> _ Public Class DataTable Inherits MarshalByValueComponent Implements IListSource, ISupportInitializeNotification, ISupportInitialize, ISerializable, _ IXmlSerializable
[SerializableAttribute] public class DataTable : MarshalByValueComponent, IListSource, ISupportInitializeNotification, ISupportInitialize, ISerializable, IXmlSerializable
[SerializableAttribute] public ref class DataTable : public MarshalByValueComponent, IListSource, ISupportInitializeNotification, ISupportInitialize, ISerializable, IXmlSerializable
DataTable は ADO.NET ライブラリ内の中心的オブジェクトです。DataTable を使用するその他のオブジェクトとしては、DataSet と DataView があります。
DataTable オブジェクトにアクセスするときは、条件付きで大文字と小文字が区別されることに注意してください。たとえば、"mydatatable" という名前の DataTable と "Mydatatable" という名前のテーブルがある場合は、この 2 つのテーブルのどちらかを検索する文字列は大文字と小文字を区別すると見なされます。ただし、"mydatatable" という名前は存在するが "Mydatatable" という名前が存在しない場合は、検索文字列は大文字と小文字を区別しないと見なされます。DataSet に、TableName プロパティの値が同じで Namespace プロパティの値が異なる DataTable オブジェクトを 2 つ格納できます。DataTable オブジェクトの使用の詳細については、「DataTable の作成」を参照してください。
プログラムによって DataTable を作成する場合は、最初に DataColumn オブジェクトを (Columns プロパティを使用してアクセスする) DataColumnCollection に追加してテーブルのスキーマを定義する必要があります。DataColumn オブジェクトの追加の詳細については、「テーブルへの列の追加」を参照してください。
DataTable に行を追加するには、最初に NewRow メソッドを使用して新しい DataRow オブジェクトを返す必要があります。スキーマはテーブルの DataColumnCollection によって定義されているため、NewRow メソッドは DataTable のスキーマを使用して行を返します。DataTable が格納できる最大行数は 16,777,216 行です。詳細については、「テーブルへのデータの追加」を参照してください。
また、DataTable には、データの整合性の保持に使用できる Constraint オブジェクトのコレクションも格納されます。詳細については、「テーブルへの制約の追加」を参照してください。
テーブルへの変更を調べるために使用できる DataTable イベントはたくさんあります。たとえば、RowChanged、RowChanging、RowDeleting、RowDeleted などがあります。DataTable で使用できるイベントの詳細については、「DataTable イベントの使用」を参照してください。
DataTable のインスタンスを作成すると、一部の読み書き可能プロパティが初期値に設定されます。これらの初期値の一覧については、System.Data.DataTable コンストラクタのトピックを参照してください。
メモ |
---|
DataSet オブジェクトと DataTable オブジェクトは MarshalByValueComponent から継承し、.NET Framework リモート処理用の ISerializable インターフェイスをサポートします。.NET Framework リモート処理に使用できる ADO.NET オブジェクトは、これらのオブジェクトだけです。 |
2 つの DataTable オブジェクトと 1 つの DataRelation オブジェクトを作成し、新しいオブジェクトを DataSet に追加する例を次に示します。テーブルは、次に DataGridView コントロールに表示されます。
' Put the next line into the Declarations section. private dataSet As DataSet Private Sub MakeDataTables() ' Run all of the functions. MakeParentTable() MakeChildTable() MakeDataRelation() BindToDataGrid() End Sub Private Sub MakeParentTable() ' Create a new DataTable. Dim table As DataTable = new DataTable("ParentTable") ' Declare variables for DataColumn and DataRow objects. Dim column As DataColumn Dim row As DataRow ' Create new DataColumn, set DataType, ColumnName ' and add to DataTable. column = New DataColumn() column.DataType = System.Type.GetType("System.Int32") column.ColumnName = "id" column.ReadOnly = True column.Unique = True ' Add the Column to the DataColumnCollection. table.Columns.Add(column) ' Create second column. column = New DataColumn() column.DataType = System.Type.GetType("System.String") column.ColumnName = "ParentItem" column.AutoIncrement = False column.Caption = "ParentItem" column.ReadOnly = False column.Unique = False ' Add the column to the table. table.Columns.Add(column) ' Make the ID column the primary key column. Dim PrimaryKeyColumns(0) As DataColumn PrimaryKeyColumns(0)= table.Columns("id") table.PrimaryKey = PrimaryKeyColumns ' Instantiate the DataSet variable. dataSet = New DataSet() ' Add the new DataTable to the DataSet. dataSet.Tables.Add(table) ' Create three new DataRow objects and add ' them to the DataTable Dim i As Integer For i = 0 to 2 row = table.NewRow() row("id") = i row("ParentItem") = "ParentItem " + i.ToString() table.Rows.Add(row) Next i End Sub Private Sub MakeChildTable() ' Create a new DataTable. Dim table As DataTable = New DataTable("childTable") Dim column As DataColumn Dim row As DataRow ' Create first column and add to the DataTable. column = New DataColumn() column.DataType= System.Type.GetType("System.Int32") column.ColumnName = "ChildID" column.AutoIncrement = True column.Caption = "ID" column.ReadOnly = True column.Unique = True ' Add the column to the DataColumnCollection. table.Columns.Add(column) ' Create second column. column = New DataColumn() column.DataType= System.Type.GetType("System.String") column.ColumnName = "ChildItem" column.AutoIncrement = False column.Caption = "ChildItem" column.ReadOnly = False column.Unique = False table.Columns.Add(column) ' Create third column. column = New DataColumn() column.DataType= System.Type.GetType("System.Int32") column.ColumnName = "ParentID" column.AutoIncrement = False column.Caption = "ParentID" column.ReadOnly = False column.Unique = False table.Columns.Add(column) dataSet.Tables.Add(table) ' Create three sets of DataRow objects, five rows each, ' and add to DataTable. Dim i As Integer For i = 0 to 4 row = table.NewRow() row("childID") = i row("ChildItem") = "Item " + i.ToString() row("ParentID") = 0 table.Rows.Add(row) Next i For i = 0 to 4 row = table.NewRow() row("childID") = i + 5 row("ChildItem") = "Item " + i.ToString() row("ParentID") = 1 table.Rows.Add(row) Next i For i = 0 to 4 row = table.NewRow() row("childID") = i + 10 row("ChildItem") = "Item " + i.ToString() row("ParentID") = 2 table.Rows.Add(row) Next i End Sub Private Sub MakeDataRelation() ' DataRelation requires two DataColumn ' (parent and child) and a name. Dim parentColumn As DataColumn = _ dataSet.Tables("ParentTable").Columns("id") Dim childColumn As DataColumn = _ dataSet.Tables("ChildTable").Columns("ParentID") Dim relation As DataRelation = new _ DataRelation("parent2Child", parentColumn, childColumn) dataSet.Tables("ChildTable").ParentRelations.Add(relation) End Sub Private Sub BindToDataGrid() ' Instruct the DataGrid to bind to the DataSet, with the ' ParentTable as the topmost DataTable. DataGrid1.SetDataBinding(dataSet,"ParentTable") End Sub
// Put the next line into the Declarations section. private System.Data.DataSet dataSet; private void MakeDataTables() { // Run all of the functions. MakeParentTable(); MakeChildTable(); MakeDataRelation(); BindToDataGrid(); } private void MakeParentTable() { // Create a new DataTable. System.Data.DataTable table = new DataTable("ParentTable"); // Declare variables for DataColumn and DataRow objects. DataColumn column; DataRow row; // Create new DataColumn, set DataType, // ColumnName and add to DataTable. column = new DataColumn(); column.DataType = System.Type.GetType("System.Int32"); column.ColumnName = "id"; column.ReadOnly = true; column.Unique = true; // Add the Column to the DataColumnCollection. table.Columns.Add(column); // Create second column. column = new DataColumn(); column.DataType = System.Type.GetType("System.String"); column.ColumnName = "ParentItem"; column.AutoIncrement = false; column.Caption = "ParentItem"; column.ReadOnly = false; column.Unique = false; // Add the column to the table. table.Columns.Add(column); // Make the ID column the primary key column. DataColumn[] PrimaryKeyColumns = new DataColumn[1]; PrimaryKeyColumns[0] = table.Columns["id"]; table.PrimaryKey = PrimaryKeyColumns; // Instantiate the DataSet variable. dataSet = new DataSet(); // Add the new DataTable to the DataSet. dataSet.Tables.Add(table); // Create three new DataRow objects and add // them to the DataTable for (int i = 0; i<= 2; i++) { row = table.NewRow(); row["id"] = i; row["ParentItem"] = "ParentItem " + i; table.Rows.Add(row); } } private void MakeChildTable() { // Create a new DataTable. DataTable table = new DataTable("childTable"); DataColumn column; DataRow row; // Create first column and add to the DataTable. column = new DataColumn(); column.DataType= System.Type.GetType("System.Int32"); column.ColumnName = "ChildID"; column.AutoIncrement = true; column.Caption = "ID"; column.ReadOnly = true; column.Unique = true; // Add the column to the DataColumnCollection. table.Columns.Add(column); // Create second column. column = new DataColumn(); column.DataType= System.Type.GetType("System.String"); column.ColumnName = "ChildItem"; column.AutoIncrement = false; column.Caption = "ChildItem"; column.ReadOnly = false; column.Unique = false; table.Columns.Add(column); // Create third column. column = new DataColumn(); column.DataType= System.Type.GetType("System.Int32"); column.ColumnName = "ParentID"; column.AutoIncrement = false; column.Caption = "ParentID"; column.ReadOnly = false; column.Unique = false; table.Columns.Add(column); dataSet.Tables.Add(table); // Create three sets of DataRow objects, // five rows each, and add to DataTable. for(int i = 0; i <= 4; i ++) { row = table.NewRow(); row["childID"] = i; row["ChildItem"] = "Item " + i; row["ParentID"] = 0 ; table.Rows.Add(row); } for(int i = 0; i <= 4; i ++) { row = table.NewRow(); row["childID"] = i + 5; row["ChildItem"] = "Item " + i; row["ParentID"] = 1 ; table.Rows.Add(row); } for(int i = 0; i <= 4; i ++) { row = table.NewRow(); row["childID"] = i + 10; row["ChildItem"] = "Item " + i; row["ParentID"] = 2 ; table.Rows.Add(row); } } private void MakeDataRelation() { // DataRelation requires two DataColumn // (parent and child) and a name. DataColumn parentColumn = dataSet.Tables["ParentTable"].Columns["id"]; DataColumn childColumn = dataSet.Tables["ChildTable"].Columns["ParentID"]; DataRelation relation = new DataRelation("parent2Child", parentColumn, childColumn); dataSet.Tables["ChildTable"].ParentRelations.Add(relation); } private void BindToDataGrid() { // Instruct the DataGrid to bind to the DataSet, with the // ParentTable as the topmost DataTable. dataGrid1.SetDataBinding(dataSet,"ParentTable"); }
System.ComponentModel.MarshalByValueComponent
System.Data.DataTable
プラットフォーム
Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。
参照
- DataTable クラスのページへのリンク