14.19 Merging DataSets

 <  Day Day Up  >  

14.19 Merging DataSet s

You want to merge the data from one DataSet object with information from another DataSet .


Technique

You merge two DataSet s by calling the Merge method from the target DataSet . The target in this case is the DataSet that will contain the final merged data. You can merge data into a DataSet by using another DataSet , a DataTable , a collection of DataRow objects, or a single DataRow . You can specify a Boolean value to control whether current changes within the target DataSet are preserved. For instance, if the target DataSet contains a row that has been modified, and a source DataSet contains a row with the same primary key with different values, specifying true preserves the target DataRow and false overwrites that row with data from the source DataSet . Finally, the last optional parameter you can specify is a value from the MissingSchemaAction enumerated data type. This parameter controls the action to take should the schemas of the two DataSets differ . A value of MissingSchemaAction.Add , for instance, adds a new column to the target DataSet if it does not exist. MissingSchemaAction.AddWithKey also adds the new column to the target DataSet and additionally creates primary keys if any columns are marked as such within the source DataSet . A value of MissingSchemaAction.Error throws an InvalidOperationException if a column exists in the source DataSet but not in the target DataSet , whereas the value MissingSchemaAction.Ignore simply discards the data from the source DataSet .

The following source-code example is a continuation of the sample application created in the last couple sections. The method shown here is an event handler for a menu item that will merge a DataSet created from an XML file to the DataSet currently used as a data source for a DataGrid control:

 
 private void mnuMerge_Click(object sender, System.EventArgs e) {     DataSet mergeData = new DataSet();     if( openFileDialog1.ShowDialog() == DialogResult.OK )     {         try         {             mergeData.ReadXml( openFileDialog1.FileName, XmlReadMode.Auto );             productsDS1.Merge( mergeData, true, MissingSchemaAction.Error );             dataGrid1.SetDataBinding( productsDS1, "Products" );         }         catch( Exception ex )         {             MessageBox.Show( ex.Message );         }     } } 

Comments

Passing around DataSet objects that contain large amounts of data might not be the best solution for objects that communicate with one another, especially if that communication occurs remotely. For instance, a DataSet object is initially filled from a middle- tier component and sent to a receiving object. The DataSet itself might be quite large, so continually passing it back and forth between the two objects could take a performance hit. Instead, you should use merging to your benefit. After you send the initial DataSet to the receiving object, you should handle the rest of the communication by only sending and receiving any changes to the DataSet , which you find by calling the GetChanges method defined in the DataSet class. It only returns the subset of records that have changed and places them into a new DataSet object. You can then send and merge this object using the techniques described in this section to the original DataSet .

 <  Day Day Up  >  


Microsoft Visual C# .Net 2003
Microsoft Visual C *. NET 2003 development skills Daquan
ISBN: 7508427505
EAN: 2147483647
Year: 2003
Pages: 440

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net