none
fastest way of having batch updates RRS feed

  • Question

  • 1. I have C# code (running on MT server), which Invokes a third party Web Service and get response data as XML structure like.
    <row id="1" field1="value" field2="value" field3="value"/>
    <row id="2" field1="value" field2="value" field3="value"/>
    <row id="3" field1="value" field2="value" field3="value"/>
    ........
    <row id="N" field1="value" field2="value" field3="value"/>

    2. based on the data in XML i have to update some columns for all rows
    update table set field2="valueFromXMl" where rowId id(1,3)

    3. What is the fastest/best approach for doing this?


    Regards
    singhhome
    Tuesday, March 24, 2009 6:31 PM

Answers

  • Use the DataView to create an internal index then use DataView.Find (which uses a binary search).

    DataView view = new DataView(dataset.table0, null, "field2", DataViewRowState.CurrentRows);  
    foreach(node in filteredNodes)   
    {  
        // find the first row that may exist for a given value  
        int index = view.Find(node.attributes["field2"].Value);  
        if (0 <= index)  
        {  
            view[index][field3] = node.attributes["field3"].value;  
        }  
     
        or  
     
        // more than 1 row may exist for a given value  
        DataRowView[] rows = view.FindRows(node.attributes["field2"].Value);  
        foreach(DataRowView row in rows)  
        {  
            view[field3] = node.attributes["field3"].value;  
        }  

    Depending on the size of your data and the number of updates, it may be faster to do a linear searches than to create a DataView.  i.e. 1 update - use linear search, 10000 updates - binary search.

    If you don't want to use a DataView, consider using System.Collections.Generic.SortedDictionary with your field2 value as the key and the DataRow as the value.
    This posting is provided "AS IS" with no warranties, and confers no rights. Please remember to mark the replies as answers if they help.
    Friday, March 27, 2009 11:08 PM
    Moderator

All replies

  • Linq to Xml may be your friend here.

    int[] ids = new int[] { 1, 3};
    XDocument xdoc = XDocument.Load(...);
    IEnumerable<XElement> elements = xdoc.Elements("row").Where(x => ids.Contains(Int32.Parse(x.Attribute("id"))));
    foreach(XElement element in elements) {
      element.Attribute("field2").SetValue("newvalue");
    }

    This posting is provided "AS IS" with no warranties, and confers no rights. Please remember to mark the replies as answers if they help.
    Tuesday, March 24, 2009 6:58 PM
    Moderator
  • may be I was not clear in my question.

    I have an xml, from xml I have to update data in dataset.

    I will select nodes from xml based on some criteria, and for returned nodes i have to update dataset rows (which match the value in the filtered xml nodes).

    filteredNodes = xml.someCriteria();

    foreach (node in filteredNodes)
    {
           //datatable row whose value in field2 is same as value of   field2  in Xml
            datarow = dataset.table0.Row[field2].value == node.attributes["field2"].value 

          datarow[field3] = node.attributes["field3"].value;

     }

    Regards
    singhhome
    Friday, March 27, 2009 8:43 PM
  • Use the DataView to create an internal index then use DataView.Find (which uses a binary search).

    DataView view = new DataView(dataset.table0, null, "field2", DataViewRowState.CurrentRows);  
    foreach(node in filteredNodes)   
    {  
        // find the first row that may exist for a given value  
        int index = view.Find(node.attributes["field2"].Value);  
        if (0 <= index)  
        {  
            view[index][field3] = node.attributes["field3"].value;  
        }  
     
        or  
     
        // more than 1 row may exist for a given value  
        DataRowView[] rows = view.FindRows(node.attributes["field2"].Value);  
        foreach(DataRowView row in rows)  
        {  
            view[field3] = node.attributes["field3"].value;  
        }  

    Depending on the size of your data and the number of updates, it may be faster to do a linear searches than to create a DataView.  i.e. 1 update - use linear search, 10000 updates - binary search.

    If you don't want to use a DataView, consider using System.Collections.Generic.SortedDictionary with your field2 value as the key and the DataRow as the value.
    This posting is provided "AS IS" with no warranties, and confers no rights. Please remember to mark the replies as answers if they help.
    Friday, March 27, 2009 11:08 PM
    Moderator