[GIS] Copy features between file geodatabase and ArcSde by using ArcObjects

arcgis-10.0arcobjectsenterprise-geodatabaseesri-geodatabasefile-geodatabase

I want to transfer all contents of a table from file geodatabase (*.gdb) to a table in ArcSde. In fact the data is meant to be replaced, so I need also to clear the table in ArcSde. Both tables have same structure. The target table is used in a map service.

I started with ConvertFeatureClass and the effects were pretty good – it was working, but each time it was creating a new table. Every try to append the data to already existing table ended with errors. Here's my code – http://pastebin.com/RWhMTe4p

Since this was not doing what I wanted, I tried something different. This time, I used IGeoDBDataTransfer interface. My efforts ended up with an error being thrown on the final Transfer method. That's my code – http://pastebin.com/MG3KUHdS The COMException is: "Error -2147216061: Exception from HRESULT: 0x80041543

Can someone please explain how to do this task?

Best Answer

As John has stated you probably have a lock on your database from the map service. If your feature class is not versioned and the features you are working with are not complex (i.e. they do not participate in a relationship class or a topology), you can do the following safely without interrupting your ArcSDE users:

  1. Use ISchemaLock to get an exclusive lock on the feature class.

    IMPORTANT: If you get a schema lock on a feature class it will put a lock on the entire feature dataset! So you may want to move all of this data into its own feature dataset if possible.

  2. Use ITableWrite2.Truncate to clear all of the data out of your table.

  3. Set IFeatureClassLoad.LoadOnlyMode to "True" on your SDE feature class. This will improve performance.

  4. Use a combination of search and insert cursors to loop over every row in your .gdb and insert into your ArcSDE feature class. Note that you don't necessarily have to use the FeatureBuffer/GeometryList in the example.

  5. Explicitly release your cursors using the normal System.Runtime.InteropServices.Marshal.ReleaseComObject(featCursor); method, then set IfeatureClassLoad.LoadOnlyMode to "False".

  6. Release your lock.

Related Question