private void DemonstrateMerge ( ) {
// 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;
// create variable for temporary DataSet.
DataSet xSet;
// add columns to table, and table to DataSet.
tbl.Columns.Add ( c1 );
tbl.Columns.Add ( c2 );
ds.Tables.Add ( tbl );
// 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" );
// change two row values.
tbl.Rows [ 0 ] [ "Item" ] = 50;
tbl.Rows [ 1 ] [ "Item" ] = 111;
// add one row.
row = tbl.NewRow ( );
row [ "Item" ] = 74;
tbl.Rows.Add ( row );
// insert code for error checking. Here we set one row in error.
tbl.Rows [ 1 ].RowError = "over 100";
PrintValues ( ds, "Modified and New Values" );
// if the table has changes or errors, create a subset DataSet.
if ( ds.HasChanges ( DataRowState.Modified | DataRowState.Added )
& ds.HasErrors ) {
// use GetChanges to extract subset.
xSet = ds.GetChanges ( DataRowState.Modified|DataRowState.Added );
PrintValues ( xSet, "Subset values" );
// insert code to reconcile errors. In this case, we'll reject changes.
foreach ( DataTable xTable in xSet.Tables ) {
if ( xTable.HasErrors ) {
foreach ( DataRow xRow in xTable.Rows ) {
// Response.Write ( xRow [ "Item" ] );
if ( ( int ) xRow [ "Item",DataRowVersion.Current ] > 100 ) {
xRow.RejectChanges ( );
xRow.ClearErrors ( );
}
}
}
}
PrintValues ( xSet, "Reconciled subset values" );
// merge changes back to first DataSet.
ds.Merge ( xSet );
PrintValues ( ds, "Merged Values" );
}
}
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 DemonstrateMerge ( )
' 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
' create variable for temporary DataSet.
Dim xSet As DataSet
' add columns to table, and table to DataSet.
tbl.Columns.Add ( c1 )
tbl.Columns.Add ( c2 )
ds.Tables.Add ( tbl )
' 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" )
' change two row values.
tbl.Rows ( 0 ) ( "Item" ) = 50
tbl.Rows ( 1 ) ( "Item" ) = 111
' add one row.
row = tbl.NewRow ( )
Row ( "Item" ) = 74
tbl.Rows.Add ( row )
' insert code for error checking. Here we set one row in error.
tbl.Rows ( 1 ).RowError = "over 100"
PrintValues ( ds, "Modified and New Values" )
' if the table has changes or errors, create a subset DataSet.
If ds.HasChanges ( DataRowState.Modified Or DataRowState.Added ) _
and ds.HasErrors Then
' Use GetChanges to extract subset.
xSet = ds.GetChanges ( DataRowState.Modified Or DataRowState.Added )
PrintValues ( xSet, "Subset values" )
' insert code to reconcile errors. In this case, we'll reject changes.
Dim xTable As DataTable
For Each xTable In xSet.Tables
If xTable.HasErrors Then
Dim xRow As DataRow
For Each xRow In xTable.Rows
' Response.Write ( xRow [ "Item" ] );
If CInt ( xRow ( "Item", DataRowVersion.Current ) ) > 100 Then
xRow.RejectChanges ( )
xRow.ClearErrors ( )
End If
Next xRow
End If
Next xTable
PrintValues ( xSet, "Reconciled subset values" )
' merge changes back to first DataSet.
ds.Merge ( xSet )
PrintValues ( ds, "Merged Values" )
End If
End Sub
Private Sub PrintValues ( ds As DataSet, label As String )
Response.Write ( ControlChars.Cr + label )
Dim tbl As DataTable
For Each tbl In ds.Tables
Response.Write ( "TableName: " + tbl.TableName )
Dim row As DataRow
For Each row In tbl.Rows
Dim col As DataColumn
For Each col In tbl.Columns
Response.Write ( ( ControlChars.Tab + " " + Row ( col ).ToString ( ) )
Next col
Response.Write ( )
Next row
Next tbl
End Sub |