wiki:CodeSnippets/BatchInsert

Version 1 (modified by jng, 16 years ago) ( diff )

--

This page is an FDO Code Snippet. Visit the CodeSnippets page to view more!

Using batch insert

Using a batched insert is a more efficent method of inserting features in bulk instead of using the regular insert. For providers like KingOracle, batched inserts can be up to 10x faster than regular inserts.

With a regular insert the process is as follows:

  1. Create the IInsert command, set its feature class
  2. Add each value to insert as a PropertyValue into the command's PropertyValues collection, referencing the property name this value will be put into.
  3. Execute the command

With a batched insert, the process is instead:

  1. Create the IInsert command, set its feature class
  2. Add a parameter for each value that will be inserted as a batch into the command's PropertyValues collection
  3. For each feature to be inserted
    1. Create a ParameterValueCollection
    2. For each value in the feature to be inserted
      1. Add a ParameterValue containing the value to insert and referencing the parameter to be inserted into (from step 2)
    3. Add this ParameterValueCollection into the command's BatchParameterValues collection
  4. Execute the command

The sample below illustrates this procedure with a list of Parcel objects. This assumes the feature source we are inserting into has the following feature class properties:

  • Name : string
  • Address : string
  • LandValue : double

C# Example

  class Parcel
  {
      public string Name;
      public string Address;
      public double LandValue;
  }

  ...

  IInsert insertCmd = ...;  
  List<Parcel> parcels = ...;
  
  //Add parameter PName as a placeholder for property "Name"
  insertCmd.PropertyValues.Add(new PropertyValue("Name", new Parameter("PName")));
  //Add parameter PAddress as a placeholder for property "PAddress"
  insertCmd.PropertyValues.Add(new PropertyValue("Address", new Parameter("PAddress")));
  //Add parameter PLandValue as a placeholder for property "PLandValue"
  insertCmd.PropertyValues.Add(new PropertyValue("LandValue", new Parameter("PLandValue")));

  //Loop through the list of parcels to insert
  foreach(Parcel p in parcels)
  {
      ParameterValueCollection paramVals = new ParameterValueCollection();

      //Insert the parcel's name under the parameter "PName"
      paramVals.Add(new ParameterValue("PName", new StringValue(p.Name)));

      //Insert the parcel's address under the parameter "PAddress"
      paramVals.Add(new ParameterValue("PAddress", new StringValue(p.Address)));

      //Insert the parcel's land value under the parameter "PLandValue"
      paramVals.Add(new ParameterValue("PLandValue", new DoubleValue(p.LandValue)));

      insertCmd.BatchParameterValues.Add(paramVals);
  }

  //Execute the batched insert
  using(IFeatureReader reader = insertCmd.Execute())
  {
      reader.Close();
  }

Note: See TracWiki for help on using the wiki.