DataTable.Merge メソッドとは? わかりやすく解説

DataTable.Merge メソッド (DataTable)

メモ : このメソッドは、.NET Framework version 2.0新しく追加されたものです。

指定した DataTable現在の DataTableマージます。

名前空間: System.Data
アセンブリ: System.Data (system.data.dll 内)
構文構文

Public Sub Merge ( _
    table As DataTable _
)
Dim instance As DataTable
Dim table As DataTable

instance.Merge(table)
public void Merge (
    DataTable table
)
public:
void Merge (
    DataTable^ table
)
public void Merge (
    DataTable table
)

パラメータ

table

現在の DataTable とマージする DataTable

解説解説

Merge メソッド使用してスキーマがほとんど同様の 2 つDataTable オブジェクトマージます。マージは、通常データ ソースから既存DataTable直前変更組み込むために、クライアント アプリケーション使用されます。これにより、クライアント アプリケーション更新されDataTable と、データ ソースからの最新データ入手できます

マージ操作では、元のテーブルおよびマージするテーブルだけが考慮されます。テーブル影響を受けること追加されることもありません。テーブルに、リレーションシップ一部として定義され1 つ上のテーブル存在する場合、各子テーブル個別マージする必要があります

通常Merge メソッドは、変更の検証エラー調整変更されデータ ソース更新、および最後に既存DataTable更新関係する一連のプロシージャ終わり呼び出されます。

マージ実行する場合既定では、マージ操作中はマージ前に既存データ加えられ変更保持されます。この動作変更するには、このメソッドの他の 2 つオーバーロードどちらか呼び出しpreserveChanges パラメータfalse指定します

クライアント アプリケーションには通常変更されデータ収集し中間層コンポーネントに戻す前に検証するためにクリックできる単一ボタンあります。このシナリオでは、初めに GetChanges メソッド呼び出します。このメソッドは、検証マージのために最適化された 2 番目の DataTable返します2 番目の DataTable オブジェクトには、変更された DataRow オブジェクトだけが含まれ、元の DataTableサブセットなります。このサブセット一般的に小さいため、中間層コンポーネント効率よく渡されます。次に中間層コンポーネントは、ストアド プロシージャ通じて変更のある元のデータ ソース更新します次に中間層は (元のクエリを再び実行することによって) 元のデータデータ ソースからの最新データを含む新しDataTable返信するか、データ ソースから変更加えられサブセット返信できます。たとえば、データ ソース自動的に一意主キー値を作成する場合、その値はクライアント アプリケーション反映できます。どちらの場合にも、返されDataTable は、Merge メソッドで、クライアント アプリケーションの元の DataTableマージできます

新しソース DataTableターゲットマージする場合、DataRowState 値が UnchangedModified、または Deleted であるすべてのソース行が、同じ主キー値を持つターゲット行と照合されます。DataRowState 値が Addedソース行は、新しソース行と同じ主キー値を持つ新しターゲット行と照合されます。

使用例使用例

次に示すコンソール アプリケーションの例では、単純な DataTable作成して、このテーブルデータ追加します次にテーブルコピー作成し、そのコピーに行を追加します最後に Merge メソッド呼び出し最初テーブルデータ2 番目のテーブルデータマージます。

Private Sub DemonstrateMergeTable()
  Dim table1 As New DataTable("Items")

  ' Add columns
  Dim column1 As New DataColumn("id",
 GetType(System.Int32))
  Dim column2 As New DataColumn("item",
 GetType(System.Int32))
  table1.Columns.Add(column1)
  table1.Columns.Add(column2)

  ' Set the primary key column.
  table1.PrimaryKey = New DataColumn() {column1}

  ' Add RowChanged event handler for the table.
  AddHandler table1.RowChanged, AddressOf Row_Changed

  ' Add some rows.
  Dim row As DataRow
  For i As Integer = 0 To
 3
    row = table1.NewRow()
    row("id") = i
    row("item") = i
    table1.Rows.Add(row)
  Next i

  ' Accept changes.
  table1.AcceptChanges()
  PrintValues(table1, "Original values")

  ' Create a second DataTable identical to the first.
  Dim table2 As DataTable = table1.Clone()

  ' Add three rows. Note that the id column can't be the 
  ' same as existing rows in the original table.
  row = table2.NewRow()
  row("id") = 14
  row("item") = 774
  table2.Rows.Add(row)

  row = table2.NewRow()
  row("id") = 12
  row("item") = 555
  table2.Rows.Add(row)

  row = table2.NewRow()
  row("id") = 13
  row("item") = 665
  table2.Rows.Add(row)

  ' Merge table2 into the table1.
  Console.WriteLine("Merging")
  table1.Merge(table2)
  PrintValues(table1, "Merged With table1")

End Sub

Private Sub Row_Changed(ByVal
 sender As Object, _
  ByVal e As DataRowChangeEventArgs)
  Console.WriteLine("Row changed {0}{1}{2}", _
    e.Action, ControlChars.Tab, e.Row.ItemArray(0))
End Sub

Private Sub PrintValues(ByVal
 table As DataTable, _
  ByVal label As String)
  ' Display the values in the supplied DataTable:
  Console.WriteLine(label)
  For Each row As DataRow
 In table.Rows
    For Each col As DataColumn
 In table.Columns
      Console.Write(ControlChars.Tab + " " + row(col).ToString())
    Next col
    Console.WriteLine()
  Next row
End Sub
private static void DemonstrateMergeTable()
{
    DataTable table1 = new DataTable("Items");

    // Add columns
    DataColumn column1 = new DataColumn("id", typeof(System.Int32));
    DataColumn column2 = new DataColumn("item", typeof(System.Int32));
    table1.Columns.Add(column1);
    table1.Columns.Add(column2);

    // Set the primary key column.
    table1.PrimaryKey = new DataColumn[] { column1 };

    // Add RowChanged event handler for the table.
    table1.RowChanged += 
        new System.Data.DataRowChangeEventHandler(Row_Changed);

    // Add some rows.
    DataRow row;
    for (int i = 0; i <= 3; i++)
    {
        row = table1.NewRow();
        row["id"] = i;
        row["item"] = i;
        table1.Rows.Add(row);
    }

    // Accept changes.
    table1.AcceptChanges();
    PrintValues(table1, "Original values");

    // Create a second DataTable identical to the first.
    DataTable table2 = table1.Clone();

    // Add three rows. Note that the id column can't be the 
    // same as existing rows in the original table.
    row = table2.NewRow();
    row["id"] = 14;
    row["item"] = 774;
    table2.Rows.Add(row);

    row = table2.NewRow();
    row["id"] = 12;
    row["item"] = 555;
    table2.Rows.Add(row);

    row = table2.NewRow();
    row["id"] = 13;
    row["item"] = 665;
    table2.Rows.Add(row);

    // Merge table2 into the table1.
    Console.WriteLine("Merging");
    table1.Merge(table2);
    PrintValues(table1, "Merged With table1");

}

private static void Row_Changed(object
 sender, 
    DataRowChangeEventArgs e)
{
    Console.WriteLine("Row changed {0}\t{1}", 
        e.Action, e.Row.ItemArray[0]);
}

private static void PrintValues(DataTable
 table, string label)
{
    // Display the values in the supplied DataTable:
    Console.WriteLine(label);
    foreach (DataRow row in table.Rows)
    {
        foreach (DataColumn col in table.Columns)
        {
            Console.Write("\t " + row[col].ToString());
        }
        Console.WriteLine();
    }
}
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照

DataTable.Merge メソッド

指定した DataTable を現在の DataTableマージます。
オーバーロードの一覧オーバーロードの一覧

名前 説明
DataTable.Merge (DataTable) 指定した DataTable現在の DataTableマージます。

.NET Compact Framework によってサポートされています。

DataTable.Merge (DataTable, Boolean) 指定した DataTable現在の DataTableマージし、現在の DataTable変更保持するかどうか指定します

.NET Compact Framework によってサポートされています。

DataTable.Merge (DataTable, Boolean, MissingSchemaAction) 指定した DataTable現在の DataTableマージして、現在の DataTable変更保持するかどうか指定し、さらに欠けているスキーマ処理方法指定します

.NET Compact Framework によってサポートされています。

参照参照

関連項目

DataTable クラス
DataTable メンバ
System.Data 名前空間

その他の技術情報

DataTable の作成使用
DataTable の作成使用
DataTable の作成使用

DataTable.Merge メソッド (DataTable, Boolean)

メモ : このメソッドは、.NET Framework version 2.0新しく追加されたものです。

指定した DataTable を現在の DataTableマージし、現在の DataTable変更保持するかどうか指定します

名前空間: System.Data
アセンブリ: System.Data (system.data.dll 内)
構文構文

Public Sub Merge ( _
    table As DataTable, _
    preserveChanges As Boolean _
)
Dim instance As DataTable
Dim table As DataTable
Dim preserveChanges As Boolean

instance.Merge(table, preserveChanges)
public void Merge (
    DataTable table,
    bool preserveChanges
)
public:
void Merge (
    DataTable^ table, 
    bool preserveChanges
)
public void Merge (
    DataTable table, 
    boolean preserveChanges
)
public function Merge (
    table : DataTable, 
    preserveChanges : boolean
)

パラメータ

table

現在の DataTableマージする DataTable

preserveChanges

現在の DataTable に対して行われた変更保持するには true保持しない場合false

解説解説

Merge メソッド使用してスキーマがほとんど同様の 2 つDataTable オブジェクトマージます。マージは、通常データ ソースから既存DataTable直前変更組み込むために、クライアント アプリケーション使用されます。これにより、クライアント アプリケーション更新されDataTable と、データ ソースからの最新データ入手できます

マージ操作では、元のテーブルおよびマージするテーブルだけが考慮されます。テーブル影響を受けること追加されることもありません。テーブルに、リレーションシップ一部として定義され1 つ上のテーブル存在する場合、各子テーブル個別マージする必要があります

通常Merge メソッドは、変更の検証エラー調整変更されデータ ソース更新、および最後に既存DataTable更新関係する一連のプロシージャ終わり呼び出されます。

マージ実行すると、preserveChanges パラメータfalse指定されていない限りマージ操作中はマージ前に既存データ加えられ変更保持されます。preserveChanges パラメータtrue設定されている場合既存の行の現在のバージョンにある既存の値は受信された値によって上書きされません。preserveChanges パラメータfalse設定されている場合既存の行の現在のバージョンにある既存の値は、受信された値によって上書きされます。行のバージョン詳細については、「行の状態とバージョン」を参照してください

クライアント アプリケーションには通常変更されデータ収集し中間層コンポーネントに戻す前に検証するためにクリックできる単一ボタンあります。このシナリオでは、初めに GetChanges メソッド呼び出します。このメソッドは、検証マージのために最適化された 2 番目の DataTable返します2 番目の DataTable オブジェクトには、変更されDataTable オブジェクトおよび DataRow オブジェクトだけが含まれ、元の DataTableサブセットなります。このサブセット一般的に小さいため、中間層コンポーネント効率よく渡されます。次に中間層コンポーネントは、ストアド プロシージャ通じて変更のある元のデータ ソース更新します次に中間層は (元のクエリを再び実行することによって) 元のデータデータ ソースからの最新データを含む新しDataTable返信するか、データ ソースから変更加えられサブセット返信できます。たとえば、データ ソース自動的に一意主キー値を作成する場合、その値はクライアント アプリケーション反映できます。どちらの場合にも、返されDataTable は、Merge メソッドで、クライアント アプリケーションの元の DataTableマージできます

新しソース DataTableターゲットマージする場合、DataRowState 値が UnchangedModified、または Deleted であるすべてのソース行が、同じ主キー値を持つターゲット行と照合されます。DataRowState 値が Addedソース行は、新しソース行と同じ主キー値を持つ新しターゲット行と照合されます。

使用例使用例

次に示すコンソール アプリケーションの例では、行を含む DataTable作成し、この行の一部データ変更して、他の DataTableデータとのマージ試みます。この例では preserveChanges パラメータさまざまな動作示します

Private Sub DemonstrateMergeTable()
  ' Demonstrate merging, within and without
  ' preserving changes.

  ' In this example, take these actions:
  ' 1. Create a DataTable (table1) and fill the table with data.
  ' 2. Create a copy of table1, and modify its data (modifiedTable).
  ' 3. Modify data in table1.
  ' 4. Make a copy of table1 (table1Copy).
  ' 5. Merge the data from modifiedTable into table1 and table1Copy,
 
  '    showing the difference between setting the preserveChanges 
  '    parameter to true and false.

  ' Create a new DataTable.
  Dim table1 As New DataTable("Items")

  ' Add two columns to the table:
  Dim column As New DataColumn("id",
 GetType(System.Int32))
  column.AutoIncrement = True
  table1.Columns.Add(column)

  column = New DataColumn("item",
 GetType(System.String))
  table1.Columns.Add(column)

  ' Set primary key column.
  table1.PrimaryKey = New DataColumn() {table1.Columns(0)}

  ' Add some rows.
  Dim row As DataRow
  For i As Integer = 0 To
 3
    row = table1.NewRow()
    row("item") = "Item "
 & i
    table1.Rows.Add(row)
  Next i

  ' Accept changes.
  table1.AcceptChanges()
  PrintValues(table1, "Original values")

  ' Using the same schema as the original table, 
  ' modify the data for later merge.
  Dim modifiedTable As DataTable = table1.Copy()
  For Each row In modifiedTable.Rows
    row("item") = row("item").ToString()
 & " modified"
  Next
  modifiedTable.AcceptChanges()

  ' Change row values, and add a new row:
  table1.Rows(0)("item") = "New
 Item 0"
  table1.Rows(1)("item") = "New
 Item 1"

  row = table1.NewRow()
  row("id") = 4
  row("item") = "Item 4"
  table1.Rows.Add(row)

  ' Get a copy of the modified data:
  Dim table1Copy As DataTable = table1.Copy()
  PrintValues(table1, "Modified and New Values")
  PrintValues(modifiedTable, "Data to be merged into table1")


  ' Merge new data into the modified data.
  table1.Merge(modifiedTable, True)
  PrintValues(table1, "Merged data (preserve changes)")

  table1Copy.Merge(modifiedTable, False)
  PrintValues(modifiedTable, "Merged data (don't preserve changes)")

End Sub

Private Sub PrintValues(ByVal
 table As DataTable, _
  ByVal label As String)

  ' Display the values in the supplied DataTable:
  Console.WriteLine(label)
  For Each row As DataRow
 In table.Rows
    For Each column As DataColumn
 In table.Columns
      Console.Write("{0}{1}", ControlChars.Tab, row(column,
 _
          DataRowVersion.Current))
    Next column
    Console.WriteLine()
  Next row
End Sub
private static void DemonstrateMergeTable()
{
    // Demonstrate merging, within and without
    // preserving changes.

    // In this example, take these actions:
    // 1. Create a DataTable (table1) and fill the table with data.
    // 2. Create a copy of table1, and modify its data (modifiedTable).
    // 3. Modify data in table1.
    // 4. Make a copy of table1 (table1Copy).
    // 5. Merge the data from modifiedTable into table1 and table1Copy,
 
    //    showing the difference between setting the preserveChanges
 
    //    parameter to true and false.

    // Create a new DataTable.
    DataTable table1 = new DataTable("Items");

    // Add two columns to the table:
    DataColumn column = new DataColumn("id", typeof(System.Int32));
    column.AutoIncrement = true;
    table1.Columns.Add(column);

    column = new DataColumn("item", typeof(System.String));
    table1.Columns.Add(column);

    // Set primary key column.
    table1.PrimaryKey = new DataColumn[] { table1.Columns[0] };

    // Add some rows.
    DataRow row;
    for (int i = 0; i <= 3; i++)
    {
        row = table1.NewRow();
        row["item"] = "Item " + i;
        table1.Rows.Add(row);
    }

    // Accept changes.
    table1.AcceptChanges();
    PrintValues(table1, "Original values");

    // Using the same schema as the original table, 
    // modify the data for later merge.
    DataTable modifiedTable = table1.Copy();
    foreach (DataRow rowModified in modifiedTable.Rows)
    {
        rowModified["item"] = rowModified["item"].ToString()
 
            + " modified";
    }
    modifiedTable.AcceptChanges();

    // Change row values, and add a new row:
    table1.Rows[0]["item"] = "new Item 0";
    table1.Rows[1]["item"] = "new Item 1";

    row = table1.NewRow();
    row["id"] = 4;
    row["item"] = "Item 4";
    table1.Rows.Add(row);

    // Get a copy of the modified data:
    DataTable table1Copy = table1.Copy();
    PrintValues(table1, "Modified and new Values");
    PrintValues(modifiedTable, "Data to be merged into table1");

    // Merge new data into the modified data.
    table1.Merge(modifiedTable, true);
    PrintValues(table1, "Merged data (preserve changes)");

    table1Copy.Merge(modifiedTable, false);
    PrintValues(modifiedTable, "Merged data (don't preserve changes)");
}


private static void PrintValues(DataTable
 table, string label)
{
    // Display the values in the supplied DataTable:
    Console.WriteLine(label);
    foreach (DataRow row in table.Rows)
    {
        foreach (DataColumn column in table.Columns)
        {
            Console.Write("\t{0}", row[column, DataRowVersion.Current]);
        }
        Console.WriteLine();
    }
}
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照

DataTable.Merge メソッド (DataTable, Boolean, MissingSchemaAction)

メモ : このメソッドは、.NET Framework version 2.0新しく追加されたものです。

指定した DataTable現在の DataTableマージして、現在の DataTable変更保持するかどうか指定し、さらに欠けているスキーマ処理方法指定します

名前空間: System.Data
アセンブリ: System.Data (system.data.dll 内)
構文構文

Public Sub Merge ( _
    table As DataTable, _
    preserveChanges As Boolean, _
    missingSchemaAction As MissingSchemaAction _
)
Dim instance As DataTable
Dim table As DataTable
Dim preserveChanges As Boolean
Dim missingSchemaAction As MissingSchemaAction

instance.Merge(table, preserveChanges, missingSchemaAction)
public void Merge (
    DataTable table,
    bool preserveChanges,
    MissingSchemaAction missingSchemaAction
)
public:
void Merge (
    DataTable^ table, 
    bool preserveChanges, 
    MissingSchemaAction missingSchemaAction
)
public void Merge (
    DataTable table, 
    boolean preserveChanges, 
    MissingSchemaAction missingSchemaAction
)
public function Merge (
    table : DataTable, 
    preserveChanges : boolean, 
    missingSchemaAction : MissingSchemaAction
)

パラメータ

table

現在の DataTable とマージする DataTable

preserveChanges

現在の DataTable に対して行われた変更保持するには true保持しない場合false

missingSchemaAction

MissingSchemaAction 値の 1 つ

解説解説

Merge メソッド使用してスキーマがほとんど同様の 2 つDataTable オブジェクトマージます。マージは、通常データ ソースから既存DataTable直前変更組み込むために、クライアント アプリケーション使用されます。これにより、クライアント アプリケーション更新されDataTable と、データ ソースからの最新データ入手できます

マージ操作では、元のテーブルおよびマージするテーブルだけが考慮されます。テーブル影響を受けること追加されることもありません。テーブルに、リレーションシップ一部として定義され1 つ上のテーブル存在する場合、各子テーブル個別マージする必要があります

通常Merge メソッドは、変更の検証エラー調整変更されデータ ソース更新、および最後に既存DataTable更新関係する一連のプロシージャ終わり呼び出されます。

マージ実行すると、preserveChanges パラメータfalse指定されていない限りマージ操作中はマージ前に既存データ加えられ変更保持されます。preserveChanges パラメータtrue設定されている場合既存の行の現在のバージョンにある既存の値は受信された値によって上書きされません。preserveChanges パラメータfalse設定されている場合既存の行の現在のバージョンにある既存の値は、受信された値によって上書きされます。行のバージョン詳細については、「行の状態とバージョン」を参照してください

クライアント アプリケーションには通常変更されデータ収集し中間層コンポーネントに戻す前に検証するためにクリックできる単一ボタンあります。このシナリオでは、初めに GetChanges メソッド呼び出します。このメソッドは、検証マージのために最適化された 2 番目の DataTable返します2 番目の DataTable オブジェクトには、変更されDataTable オブジェクトおよび DataRow オブジェクトだけが含まれ、元の DataTableサブセットなります。このサブセット一般的に小さいため、中間層コンポーネント効率よく渡されます。次に中間層コンポーネントは、ストアド プロシージャ通じて変更のある元のデータ ソース更新します次に中間層は (元のクエリを再び実行することによって) 元のデータデータ ソースからの最新データを含む新しDataTable返信するか、データ ソースから変更加えられサブセット返信できます。たとえば、データ ソース自動的に一意主キー値を作成する場合、その値はクライアント アプリケーション反映できます。どちらの場合にも、返されDataTable は、Merge メソッドで、クライアント アプリケーションの元の DataTableマージできます

Merge メソッド呼び出されたときは、スキーマ変更されている可能性があるため、2 つDataTable オブジェクトスキーマ比較されます。たとえば、B to B シナリオでは、自動処理によって新しい列XML スキーマ追加されていることがありますソース DataTableターゲット不足しているスキーマ要素 (追加された DataColumn オブジェクト) を格納している場合、そのスキーマ要素引数 missingSchemaActionMissingSchemaAction.Add設定してターゲット追加できます。この場合マージされた DataTable は、追加されスキーマデータ格納します

スキーママージ後に、データマージます。

新しソース DataTableターゲットマージする場合、DataRowState 値が UnchangedModified、または Deleted であるすべてのソース行が、同じ主キー値を持つターゲット行と照合されます。DataRowState 値が Addedソース行は、新しソース行と同じ主キー値を持つ新しターゲット行と照合されます。

使用例使用例

Merge メソッドmissingSchemaAction パラメータ動作次のコンソール アプリケーション示します。この例では同じテーブルを 2 バージョン作成し2 番目のバージョンスキーマ変更します次に2 番目のテーブルを 1 番目のテーブルマージます。

Private Sub DemonstrateMergeTable()
  Dim itemsTable As New
 DataTable("Items")

  ' Add columns
  Dim idColumn As New DataColumn("id",
 GetType(System.Int32))
  Dim itemColumn As New
 DataColumn("item", GetType(System.Int32))
  itemsTable.Columns.Add(idColumn)
  itemsTable.Columns.Add(itemColumn)

  ' Set the primary key column.
  itemsTable.PrimaryKey = New DataColumn() {idColumn}

  ' Add RowChanged event handler for the table.
  AddHandler itemsTable.RowChanged, AddressOf
 Row_Changed

  ' Add some rows.
  Dim row As DataRow
  For i As Integer = 0 To
 3
    row = itemsTable.NewRow()
    row("id") = i
    row("item") = i
    itemsTable.Rows.Add(row)
  Next i

  ' Accept changes.
  itemsTable.AcceptChanges()
  PrintValues(itemsTable, "Original values")

  ' Create a second DataTable identical to the first.
  Dim itemsClone As DataTable = itemsTable.Clone()

  ' Add column to the second column, so that the 
  ' schemas no longer match.
  itemsClone.Columns.Add("newColumn", GetType(System.String))

  ' Add three rows. Note that the id column can't be the 
  ' same as existing rows in the original table.
  row = itemsClone.NewRow()
  row("id") = 14
  row("item") = 774
  row("newColumn") = "new column
 1"
  itemsClone.Rows.Add(row)

  row = itemsClone.NewRow()
  row("id") = 12
  row("item") = 555
  row("newColumn") = "new column
 2"
  itemsClone.Rows.Add(row)

  row = itemsClone.NewRow()
  row("id") = 13
  row("item") = 665
  row("newColumn") = "new column
 3"
  itemsClone.Rows.Add(row)

  ' Merge itemsClone into the itemsTable.
  Console.WriteLine("Merging")
  itemsTable.Merge(itemsClone, False, MissingSchemaAction.Add)
  PrintValues(itemsTable, "Merged With itemsTable, Schema added")
End Sub

Private Sub Row_Changed(ByVal
 sender As Object, _
  ByVal e As DataRowChangeEventArgs)
  Console.WriteLine("Row changed {0}{1}{2}", _
    e.Action, ControlChars.Tab, e.Row.ItemArray(0))
End Sub

Private Sub PrintValues(ByVal
 table As DataTable, ByVal label As
 String)
  ' Display the values in the supplied DataTable:
  Console.WriteLine(label)
  For Each row As DataRow
 In table.Rows
    For Each col As DataColumn
 In table.Columns
      Console.Write(ControlChars.Tab + " " + row(col).ToString())
    Next col
    Console.WriteLine()
  Next row
End Sub
private static void DemonstrateMergeTable()
{
    DataTable itemsTable = new DataTable("Items");

    // Add columns
    DataColumn idColumn = new DataColumn("id", typeof(System.Int32));
    DataColumn itemColumn = new DataColumn("item", typeof(System.Int32));
    itemsTable.Columns.Add(idColumn);
    itemsTable.Columns.Add(itemColumn);

    // Set the primary key column.
    itemsTable.PrimaryKey = new DataColumn[] { idColumn };

    // Add RowChanged event handler for the table.
    itemsTable.RowChanged += 
        new System.Data.DataRowChangeEventHandler(Row_Changed);

    // Add ten rows.
    DataRow row;
    for (int i = 0; i <= 9; i++)
    {
        row = itemsTable.NewRow();
        row["id"] = i;
        row["item"] = i;
        itemsTable.Rows.Add(row);
    }

    // Accept changes.
    itemsTable.AcceptChanges();
    PrintValues(itemsTable, "Original values");

    // Create a second DataTable identical to the first.
    DataTable itemsClone = itemsTable.Clone();

    // Add column to the second column, so that the 
    // schemas no longer match.
    itemsClone.Columns.Add("newColumn", typeof(System.String));

    // Add three rows. Note that the id column can't be the 
    // same as existing rows in the original table.
    row = itemsClone.NewRow();
    row["id"] = 14;
    row["item"] = 774;
    row["newColumn"] = "new column 1";
    itemsClone.Rows.Add(row);

    row = itemsClone.NewRow();
    row["id"] = 12;
    row["item"] = 555;
    row["newColumn"] = "new column 2";
    itemsClone.Rows.Add(row);

    row = itemsClone.NewRow();
    row["id"] = 13;
    row["item"] = 665;
    row["newColumn"] = "new column 3";
    itemsClone.Rows.Add(row);

    // Merge itemsClone into the itemsTable.
    Console.WriteLine("Merging");
    itemsTable.Merge(itemsClone, false, MissingSchemaAction.Add);
    PrintValues(itemsTable, "Merged With itemsTable, schema added");
}

private static void Row_Changed(object
 sender, 
    DataRowChangeEventArgs e)
{
    Console.WriteLine("Row changed {0}\t{1}", 
        e.Action, e.Row.ItemArray[0]);
}

private static void PrintValues(DataTable
 table, string label)
{
    // Display the values in the supplied DataTable:
    Console.WriteLine(label);
    foreach (DataRow row in table.Rows)
    {
        foreach (DataColumn col in table.Columns)
        {
            Console.Write("\t " + row[col].ToString());
        }
        Console.WriteLine();
    }
}
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照



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

辞書ショートカット

カテゴリ一覧

すべての辞書の索引



Weblioのサービス

DataTable.Merge メソッドのお隣キーワード
検索ランキング

   

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



DataTable.Merge メソッドのページの著作権
Weblio 辞書 情報提供元は 参加元一覧 にて確認できます。

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

©2024 GRAS Group, Inc.RSS