asp.net.ph

DataSet.Merge Method

System.Data Namespace   DataSet Class


Merges this DataSet with a specified DataSet.

Overload List

1. Merges this DataSet with an array of DataRow objects.

2. Merges this DataSet into a specified DataSet.

3. Merges a DataSet with a specified DataTable.

4. Merges this DataSet with a specified DataSet preserving changes according to the specified argument.

5. Merges this DataSet with an array of DataRow objects, preserving changes according to the specified argument, and handling an incompatible schema according to the specified argument.

6. Merges this DataSet with a specified DataSet preserving changes according to the specified argument, and handling an incompatible schema according to the specified argument.

7. Merges this DataTable with a specified DataTable preserving changes according to the specified argument, and handling an incompatible schema according to the specified argument.


Example

The following example initializes a simple DataSet with one table, two columns, and ten rows. A second DataTable is created that is nearly identical to the first except that a new DataColumn is added to the table. Two rows are added to the second table, which is then merged into the DataSet with the preserveChanges argument set to false, and the missingSchemaAction argument set to MissingSchemaAction.Add.

NOTE: This example uses one of the overloaded versions of Merge. For other examples that may be available, see the individual overload topics.

private void DemonstrateMergeTableAddSchema ( ) {
   // create a DataSet with one table, two columns, and three rows.
   DataSet ds = new DataSet ( "myDataSet" );
   DataTable tbl = new DataTable ( "Items" );
   DataColumn c1 = new DataColumn ( "id", Type.GetType ( "System.Int32" ),"" );
   c1.AutoIncrement = true;
   DataColumn c2 = new DataColumn ( "Item", Type.GetType ( "System.Int32" ),"" );
   // dataColumn array to set primary key.
   DataColumn [ ] keyCol = new DataColumn [ 1 ];
   DataRow row;
   // add RowChanged event handler for the table.
   tbl.RowChanged+ = new DataRowChangeEventHandler ( Row_Changed );
   ds.Tables.Add ( tbl );
   tbl.Columns.Add ( c1 );
   tbl.Columns.Add ( c2 );
   // set primary key column.
   keyCol [ 0 ] = c1;
   tbl.PrimaryKey = keyCol;
   // add ten rows.
   for ( int i = 0; i <10; i++ ) {
      row = tbl.NewRow ( );
      row [ "Item" ] = i;
      tbl.Rows.Add ( row );
  }
   // accept changes.
   ds.AcceptChanges ( );
   PrintValues ( ds, "Original values" );
   // create a second DataTable identical to the first, with one extra column.
   DataTable t2 = new DataTable ( "Items" );
   // add two columns.
   DataColumn col3 = new DataColumn ( "id", Type.GetType ( "System.Int32" ) );
   DataColumn col4 = new DataColumn ( "item", Type.GetType ( "System.Int32" ) );
   DataColumn col5 = new DataColumn ( "extra",Type.GetType ( "System.String" ) );
   t2.Columns.Add ( col3 );
   t2.Columns.Add ( col4 );
   t2.Columns.Add ( col5 );
   // set primary key column.
   DataColumn [ ] t2KeyCol = new DataColumn [ 1 ];
   t2KeyCol [ 0 ] = col3;
   t2.PrimaryKey = t2KeyCol;
   // add two rows. Note that the id column must be unique.
   DataRow newRow;
   newRow = t2.NewRow ( );
   newRow [ "id" ] = 12;
   newRow [ "Item" ] = 555;
   newRow [ "extra" ] = "extra Column 1";
   t2.Rows.Add ( newRow );

   newRow = t2.NewRow ( );
   newRow [ "id" ] = 13;
   newRow [ "Item" ] = 665;
   newRow [ "extra" ] = "extra Column 2";
   t2.Rows.Add ( newRow );
   // merge the table into the DataSet.
   Response.Write ( "Merging" );
   ds.Merge ( t2,false,MissingSchemaAction.Add );
   PrintValues ( ds, "Merged With Table, Schema Added" );
}

private void Row_Changed ( object sender, DataRowChangeEventArgs e ) {
   Response.Write ( "Row Changed " + e.Action.ToString ( ) + "\tbl" + e.Row.ItemArray [ 0 ] );
}

private void PrintValues ( DataSet ds, string label ) {
   Response.Write ( "<br>" + label );

   foreach ( DataTable tbl in ds.Tables ) {
      Response.Write ( "TableName: " + tbl.TableName );

      foreach ( DataRow row in tbl.Rows ) {

         foreach ( DataColumn col in tbl.Columns ) {
            Response.Write ( "\t " + row [ col ] );
        }
         Response.Write ( );
     }
   }
}
  C# VB

See Also

DataSet Members Skip Navigation Links




Home
Suggested Reading


Previous page Back to top Next page

© 2000-2010 Rey Nuñez All rights reserved.

If you have any question, comment or suggestion
about this site, please send us a note

You can help support asp.net.ph