System.Data.DataTable クラス

この記事では、この API のリファレンス ドキュメントへの補足的な解説を提供します。

クラスは DataTable 、ADO.NET ライブラリ内の中心的なオブジェクトです。 使用するその他のDataTableオブジェクトには、DataViewDataSet

DataTable オブジェクト名は、条件付きで大文字と小文字が区別されます。 たとえば、1 つが DataTable "mydatatable" という名前で、もう 1 つが "Mydatatable" という名前の場合、テーブルの 1 つを検索するために使用される文字列は大文字と小文字が区別されます。 ただし、"mydatatable" が存在し、"Mydatatable" が存在しない場合、検索文字列は大文字と小文字を区別しないと見なされます。 A DataSet には、同じTableNameプロパティ値と異なるNamespaceプロパティ値を持つ 2 つのDataTableオブジェクトを含めることができます。 オブジェクトのDataTable操作の詳細については、「DataTable の作成」を参照してください

プログラムで作成する場合は、まずオブジェクトを DataTable (プロパティからアクセスされる) にDataColumnCollection追加DataColumnして、そのスキーマを定義するColumns必要があります。 オブジェクトの追加DataColumnの詳細については、「DataTable への列の追加」を参照してください

行を a DataTableに追加するには、最初にメソッドを NewRow 使用して新しい DataRow オブジェクトを返す必要があります。 このメソッドは NewRow 、テーブルで定義されているように、スキーマ DataTableを持つ行を返します DataColumnCollection。 格納できる行の最大数は 16,777,216 行 DataTable です。 詳細については、「DataTable へのデータの追加」を参照してください

DataTableまた、データのConstraint整合性を確保するために使用できるオブジェクトのコレクションも含まれています。 詳しくは、「DataTable の制約」をご覧ください。

テーブルに変更が加えられるタイミングを判断するために使用できるイベントは多数 DataTable あります。 これには、RowChangedRowChangingRowDeleting、および RowDeleted が含まれます。 と共に使用できるイベントの詳細については、「DataTable イベントDataTable処理」を参照してください。

インスタンス DataTable が作成されると、読み取り/書き込みプロパティの一部が初期値に設定されます。 これらの値の一覧については、コンストラクターを DataTable 参照してください。

Note

オブジェクトとDataTableオブジェクトはDataSet、.NET リモート処理のインターフェイスをMarshalByValueComponentISerializable継承し、サポートします。 これらは、.NET リモート処理に使用できる唯一の ADO.NET オブジェクトです。

セキュリティに関する考慮事項

DataSet と DataTable のセキュリティの詳細については、セキュリティ ガイダンスを参照してください

このサンプルでは、特定のスキーマ定義を使用して DataTable を手動で作成する方法を示します。

  • 複数の DataTable を作成し、初期列を定義します。
  • テーブル制約を作成します。
  • 値を挿入し、テーブルを表示します。
  • 式列を作成し、テーブルを表示します。
using System;
using System.Data;

class Program
{
    static void Main(string[] args)
    {
        // Create two tables and add them into the DataSet
        DataTable orderTable = CreateOrderTable();
        DataTable orderDetailTable = CreateOrderDetailTable();
        DataSet salesSet = new DataSet();
        salesSet.Tables.Add(orderTable);
        salesSet.Tables.Add(orderDetailTable);

        // Set the relations between the tables and create the related constraint.
        salesSet.Relations.Add("OrderOrderDetail", orderTable.Columns["OrderId"], orderDetailTable.Columns["OrderId"], true);

        Console.WriteLine("After creating the foreign key constriant, you will see the following error if inserting order detail with the wrong OrderId: ");
        try
        {
            DataRow errorRow = orderDetailTable.NewRow();
            errorRow[0] = 1;
            errorRow[1] = "O0007";
            orderDetailTable.Rows.Add(errorRow);
        }
        catch (Exception e)
        {
            Console.WriteLine(e.Message);
        }
        Console.WriteLine();

        // Insert the rows into the table
        InsertOrders(orderTable);
        InsertOrderDetails(orderDetailTable);

        Console.WriteLine("The initial Order table.");
        ShowTable(orderTable);

        Console.WriteLine("The OrderDetail table.");
        ShowTable(orderDetailTable);

        // Use the Aggregate-Sum on the child table column to get the result.
        DataColumn colSub = new DataColumn("SubTotal", typeof(Decimal), "Sum(Child.LineTotal)");
        orderTable.Columns.Add(colSub);

        // Compute the tax by referencing the SubTotal expression column.
        DataColumn colTax = new DataColumn("Tax", typeof(Decimal), "SubTotal*0.1");
        orderTable.Columns.Add(colTax);

        // If the OrderId is 'Total', compute the due on all orders; or compute the due on this order.
        DataColumn colTotal = new DataColumn("TotalDue", typeof(Decimal), "IIF(OrderId='Total',Sum(SubTotal)+Sum(Tax),SubTotal+Tax)");
        orderTable.Columns.Add(colTotal);

        DataRow row = orderTable.NewRow();
        row["OrderId"] = "Total";
        orderTable.Rows.Add(row);

        Console.WriteLine("The Order table with the expression columns.");
        ShowTable(orderTable);

        Console.WriteLine("Press any key to exit.....");
        Console.ReadKey();
    }

    private static DataTable CreateOrderTable()
    {
        DataTable orderTable = new DataTable("Order");

        // Define one column.
        DataColumn colId = new DataColumn("OrderId", typeof(String));
        orderTable.Columns.Add(colId);

        DataColumn colDate = new DataColumn("OrderDate", typeof(DateTime));
        orderTable.Columns.Add(colDate);

        // Set the OrderId column as the primary key.
        orderTable.PrimaryKey = new DataColumn[] { colId };

        return orderTable;
    }

    private static DataTable CreateOrderDetailTable()
    {
        DataTable orderDetailTable = new DataTable("OrderDetail");

        // Define all the columns once.
        DataColumn[] cols =
        {
            new DataColumn("OrderDetailId", typeof(Int32)),
            new DataColumn("OrderId", typeof(String)),
            new DataColumn("Product", typeof(String)),
            new DataColumn("UnitPrice", typeof(Decimal)),
            new DataColumn("OrderQty", typeof(Int32)),
            new DataColumn("LineTotal", typeof(Decimal), "UnitPrice*OrderQty")
        };

        orderDetailTable.Columns.AddRange(cols);
        orderDetailTable.PrimaryKey = new DataColumn[] { orderDetailTable.Columns["OrderDetailId"] };
        return orderDetailTable;
    }

    private static void InsertOrders(DataTable orderTable)
    {
        // Add one row once.
        DataRow row1 = orderTable.NewRow();
        row1["OrderId"] = "O0001";
        row1["OrderDate"] = new DateTime(2013, 3, 1);
        orderTable.Rows.Add(row1);

        DataRow row2 = orderTable.NewRow();
        row2["OrderId"] = "O0002";
        row2["OrderDate"] = new DateTime(2013, 3, 12);
        orderTable.Rows.Add(row2);

        DataRow row3 = orderTable.NewRow();
        row3["OrderId"] = "O0003";
        row3["OrderDate"] = new DateTime(2013, 3, 20);
        orderTable.Rows.Add(row3);
    }

    private static void InsertOrderDetails(DataTable orderDetailTable)
    {
        // Use an Object array to insert all the rows .
        // Values in the array are matched sequentially to the columns, based on the order in which they appear in the table.
        Object[] rows =
        {
            new Object[] { 1, "O0001", "Mountain Bike", 1419.5, 36 },
            new Object[] { 2, "O0001", "Road Bike", 1233.6, 16 },
            new Object[] { 3, "O0001", "Touring Bike", 1653.3, 32 },
            new Object[] { 4, "O0002", "Mountain Bike", 1419.5, 24 },
            new Object[] { 5, "O0002", "Road Bike", 1233.6, 12 },
            new Object[] { 6, "O0003", "Mountain Bike", 1419.5, 48 },
            new Object[] { 7, "O0003", "Touring Bike", 1653.3, 8 },
        };

        foreach (Object[] row in rows)
        {
            orderDetailTable.Rows.Add(row);
        }
    }

    private static void ShowTable(DataTable table)
    {
        foreach (DataColumn col in table.Columns)
        {
            Console.Write("{0,-14}", col.ColumnName);
        }
        Console.WriteLine();

        foreach (DataRow row in table.Rows)
        {
            foreach (DataColumn col in table.Columns)
            {
                if (col.DataType.Equals(typeof(DateTime)))
                    Console.Write("{0,-14:d}", row[col]);
                else if (col.DataType.Equals(typeof(Decimal)))
                    Console.Write("{0,-14:C}", row[col]);
                else
                    Console.Write("{0,-14}", row[col]);
            }
            Console.WriteLine();
        }
        Console.WriteLine();
    }
}
Imports System.Data

Class Program
   Public Shared Sub Main(args As String())
      ' Create two tables and add them into the DataSet
      Dim orderTable As DataTable = CreateOrderTable()
      Dim orderDetailTable As DataTable = CreateOrderDetailTable()
      Dim salesSet As New DataSet()
      salesSet.Tables.Add(orderTable)
      salesSet.Tables.Add(orderDetailTable)

      ' Set the relations between the tables and create the related constraint.
      salesSet.Relations.Add("OrderOrderDetail", orderTable.Columns("OrderId"), orderDetailTable.Columns("OrderId"), True)

      Console.WriteLine("After creating the foreign key constriant, you will see the following error if inserting order detail with the wrong OrderId: ")
      Try
         Dim errorRow As DataRow = orderDetailTable.NewRow()
         errorRow(0) = 1
         errorRow(1) = "O0007"
         orderDetailTable.Rows.Add(errorRow)
      Catch e As Exception
         Console.WriteLine(e.Message)
      End Try
      Console.WriteLine()

      ' Insert the rows into the table
      InsertOrders(orderTable)
      InsertOrderDetails(orderDetailTable)

      Console.WriteLine("The initial Order table.")
      ShowTable(orderTable)

      Console.WriteLine("The OrderDetail table.")
      ShowTable(orderDetailTable)

      ' Use the Aggregate-Sum on the child table column to get the result.
      Dim colSub As New DataColumn("SubTotal", GetType([Decimal]), "Sum(Child.LineTotal)")
      orderTable.Columns.Add(colSub)

      ' Compute the tax by referencing the SubTotal expression column.
      Dim colTax As New DataColumn("Tax", GetType([Decimal]), "SubTotal*0.1")
      orderTable.Columns.Add(colTax)

      ' If the OrderId is 'Total', compute the due on all orders; or compute the due on this order.
      Dim colTotal As New DataColumn("TotalDue", GetType([Decimal]), "IIF(OrderId='Total',Sum(SubTotal)+Sum(Tax),SubTotal+Tax)")
      orderTable.Columns.Add(colTotal)

      Dim row As DataRow = orderTable.NewRow()
      row("OrderId") = "Total"
      orderTable.Rows.Add(row)

      Console.WriteLine("The Order table with the expression columns.")
      ShowTable(orderTable)

      Console.WriteLine("Press any key to exit.....")
      Console.ReadKey()
   End Sub

   Private Shared Function CreateOrderTable() As DataTable
      Dim orderTable As New DataTable("Order")

      ' Define one column.
      Dim colId As New DataColumn("OrderId", GetType([String]))
      orderTable.Columns.Add(colId)

      Dim colDate As New DataColumn("OrderDate", GetType(DateTime))
      orderTable.Columns.Add(colDate)

      ' Set the OrderId column as the primary key.
      orderTable.PrimaryKey = New DataColumn() {colId}

      Return orderTable
   End Function

   Private Shared Function CreateOrderDetailTable() As DataTable
      Dim orderDetailTable As New DataTable("OrderDetail")

      ' Define all the columns once.
      Dim cols As DataColumn() = {New DataColumn("OrderDetailId", GetType(Int32)), New DataColumn("OrderId", GetType([String])), New DataColumn("Product", GetType([String])), New DataColumn("UnitPrice", GetType([Decimal])), New DataColumn("OrderQty", GetType(Int32)), New DataColumn("LineTotal", GetType([Decimal]), "UnitPrice*OrderQty")}

      orderDetailTable.Columns.AddRange(cols)
      orderDetailTable.PrimaryKey = New DataColumn() {orderDetailTable.Columns("OrderDetailId")}
      Return orderDetailTable
   End Function

   Private Shared Sub InsertOrders(orderTable As DataTable)
      ' Add one row once.
      Dim row1 As DataRow = orderTable.NewRow()
      row1("OrderId") = "O0001"
      row1("OrderDate") = New DateTime(2013, 3, 1)
      orderTable.Rows.Add(row1)

      Dim row2 As DataRow = orderTable.NewRow()
      row2("OrderId") = "O0002"
      row2("OrderDate") = New DateTime(2013, 3, 12)
      orderTable.Rows.Add(row2)

      Dim row3 As DataRow = orderTable.NewRow()
      row3("OrderId") = "O0003"
      row3("OrderDate") = New DateTime(2013, 3, 20)
      orderTable.Rows.Add(row3)
   End Sub

   Private Shared Sub InsertOrderDetails(orderDetailTable As DataTable)
      ' Use an Object array to insert all the rows .
      ' Values in the array are matched sequentially to the columns, based on the order in which they appear in the table.
      Dim rows As [Object]() = {New [Object]() {1, "O0001", "Mountain Bike", 1419.5, 36}, New [Object]() {2, "O0001", "Road Bike", 1233.6, 16}, New [Object]() {3, "O0001", "Touring Bike", 1653.3, 32}, New [Object]() {4, "O0002", "Mountain Bike", 1419.5, 24}, New [Object]() {5, "O0002", "Road Bike", 1233.6, 12}, New [Object]() {6, "O0003", "Mountain Bike", 1419.5, 48}, _
         New [Object]() {7, "O0003", "Touring Bike", 1653.3, 8}}

      For Each row As [Object]() In rows
         orderDetailTable.Rows.Add(row)
      Next
   End Sub

   Private Shared Sub ShowTable(table As DataTable)
      For Each col As DataColumn In table.Columns
         Console.Write("{0,-14}", col.ColumnName)
      Next
      Console.WriteLine()

      For Each row As DataRow In table.Rows
         For Each col As DataColumn In table.Columns
            If col.DataType.Equals(GetType(DateTime)) Then
               Console.Write("{0,-14:d}", row(col))
            ElseIf col.DataType.Equals(GetType([Decimal])) Then
               Console.Write("{0,-14:C}", row(col))
            Else
               Console.Write("{0,-14}", row(col))
            End If
         Next
         Console.WriteLine()
      Next
      Console.WriteLine()
   End Sub
End Class