DataTable クラスとは? わかりやすく解説

DataTable クラス

インメモリ データテーブル 1 つ表します

名前空間: System.Data
アセンブリ: 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
/** @attribute SerializableAttribute() */ 
public class DataTable extends MarshalByValueComponent
 implements IListSource, ISupportInitializeNotification, 
    ISupportInitialize, ISerializable, IXmlSerializable
SerializableAttribute 
public class DataTable extends
 MarshalByValueComponent implements IListSource, ISupportInitializeNotification,
 
    ISupportInitialize, ISerializable, IXmlSerializable
解説解説

DataTableADO.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.Object
   System.ComponentModel.MarshalByValueComponent
    System.Data.DataTable
スレッド セーフスレッド セーフ
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照



英和和英テキスト翻訳>> Weblio翻訳
英語⇒日本語日本語⇒英語
  

辞書ショートカット

すべての辞書の索引

「DataTable クラス」の関連用語

DataTable クラスのお隣キーワード
検索ランキング

   

英語⇒日本語
日本語⇒英語
   



DataTable クラスのページの著作権
Weblio 辞書 情報提供元は 参加元一覧 にて確認できます。

   
日本マイクロソフト株式会社日本マイクロソフト株式会社
© 2024 Microsoft.All rights reserved.

©2024 GRAS Group, Inc.RSS