System.Data Namespace DataSet Class
Merges this DataSet with a specified DataSet.
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.
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 ( );
}
}
}
Private Sub DemonstrateMergeTableAddSchema ( )
' create a DataSet with one table, two columns, and three rows.
Dim ds As New DataSet ( "myDataSet" )
Dim tbl As New DataTable ( "Items" )
Dim c1 As New DataColumn ( "id", Type.GetType ( "System.Int32" ), "" )
c1.AutoIncrement = True
Dim c2 As New DataColumn ( "Item", Type.GetType ( "System.Int32" ), "" )
' dataColumn array to set primary key.
Dim keyCol ( 1 ) As DataColumn
Dim row As DataRow
' add RowChanged event handler for the table.
AddHandler tbl.RowChanged, AddressOf 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.
Dim i As Integer
For i = 0 To 9
row = tbl.NewRow ( )
Row ( "Item" ) = i
tbl.Rows.Add ( row )
Next i
' accept changes.
ds.AcceptChanges ( )
PrintValues ( ds, "Original values" )
' create a second DataTable identical to the first, with
' one extra column.
Dim t2 As New DataTable ( "Items" )
' add two columns.
Dim col3 As New DataColumn ( "id", Type.GetType ( "System.Int32" ) )
Dim col4 As New DataColumn ( "item", Type.GetType ( "System.Int32" ) )
Dim col5 As New DataColumn ( "extra", Type.GetType ( "System.String" ) )
t2.Columns.Add ( col3 )
t2.Columns.Add ( col4 )
t2.Columns.Add ( col5 )
' set primary key column.
Dim t2KeyCol ( 1 ) As DataColumn
t2KeyCol ( 0 ) = col3
t2.PrimaryKey = t2KeyCol
' add two rows. Note that the id column can not be the
' same as existing rows in the DataSet table.
Dim newRow As DataRow
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" )
End Sub
Private Sub Row_Changed ( sender As Object, e As DataRowChangeEventArgs )
Response.Write ( "Row Changed " + e.Action.ToString ( ) _
+ ControlChars.Tab + e.Row.ItemArray ( 0 ).ToString ( ) )
End Sub
Private Sub PrintValues ( ds As DataSet, label As String )
Response.Write ( ControlChars.Cr + label )
Dim tbl As DataTable
Dim row As DataRow
Dim col As DataColumn
For Each tbl In ds.Tables
Response.Write ( "TableName: " + tbl.TableName )
For Each row In tbl.Rows
For Each col In tbl.Columns
Response.Write ( ControlChars.Tab + " " + Row ( col ).ToString ( ) )
Next col
Response.Write ( )
Next row
Next tbl
End Sub |
|
C# |
VB |
DataSet Members
|
|