Changes between Initial Version and Version 1 of CodeSnippets/BatchInsert


Ignore:
Timestamp:
10/29/08 07:58:27 (16 years ago)
Author:
jng
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • CodeSnippets/BatchInsert

    v1 v1  
     1[[PageOutline]]
     2
     3This page is an FDO Code Snippet.  Visit the CodeSnippets page to view more!
     4
     5= Using batch insert =
     6
     7Using 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.
     8
     9With a regular insert the process is as follows:
     10
     11 1. Create the IInsert command, set its feature class
     12 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.
     13 3. Execute the command
     14
     15With a batched insert, the process is instead:
     16
     17 1. Create the IInsert command, set its feature class
     18 2. Add a parameter for each value that will be inserted as a batch into the command's PropertyValues collection
     19 3. For each feature to be inserted
     20  a. Create a ParameterValueCollection
     21  b. For each value in the feature to be inserted
     22   i. Add a ParameterValue containing the value to insert and referencing the parameter to be inserted into (from step 2)
     23  c. Add this ParameterValueCollection into the command's BatchParameterValues collection
     24 4. Execute the command
     25
     26The 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:
     27
     28 * Name : string
     29 * Address : string
     30 * LandValue : double
     31
     32== C# Example ==
     33
     34{{{
     35#!cpp
     36
     37  class Parcel
     38  {
     39      public string Name;
     40      public string Address;
     41      public double LandValue;
     42  }
     43
     44  ...
     45
     46  IInsert insertCmd = ...; 
     47  List<Parcel> parcels = ...;
     48 
     49  //Add parameter PName as a placeholder for property "Name"
     50  insertCmd.PropertyValues.Add(new PropertyValue("Name", new Parameter("PName")));
     51  //Add parameter PAddress as a placeholder for property "PAddress"
     52  insertCmd.PropertyValues.Add(new PropertyValue("Address", new Parameter("PAddress")));
     53  //Add parameter PLandValue as a placeholder for property "PLandValue"
     54  insertCmd.PropertyValues.Add(new PropertyValue("LandValue", new Parameter("PLandValue")));
     55
     56  //Loop through the list of parcels to insert
     57  foreach(Parcel p in parcels)
     58  {
     59      ParameterValueCollection paramVals = new ParameterValueCollection();
     60
     61      //Insert the parcel's name under the parameter "PName"
     62      paramVals.Add(new ParameterValue("PName", new StringValue(p.Name)));
     63
     64      //Insert the parcel's address under the parameter "PAddress"
     65      paramVals.Add(new ParameterValue("PAddress", new StringValue(p.Address)));
     66
     67      //Insert the parcel's land value under the parameter "PLandValue"
     68      paramVals.Add(new ParameterValue("PLandValue", new DoubleValue(p.LandValue)));
     69
     70      insertCmd.BatchParameterValues.Add(paramVals);
     71  }
     72
     73  //Execute the batched insert
     74  using(IFeatureReader reader = insertCmd.Execute())
     75  {
     76      reader.Close();
     77  }
     78
     79}}}
     80