Converting an Enterpise Geodatabase Feature Class to another Enterprise Geodatabase


The following example converts an enterprise geodatabase feature class to another enterprise geodatabase.

Particular attention is payed to input field names which are qualified as Owner.TableName.FieldName This is most common when there is a field called 'Owner.Tablename.AREA', typically present in a feature class originally converted from a coverage. By default the field checker will convert the '.' to a '_', however this could result in the field width limit being hit, if the output database environment does not allow field names as wide as those of the input database environment. If just the plain, unqualified, field name (e.g 'AREA') is submitted, the feature data converter will avoid this problem.

NOTE: to successfully run this script, you will need to modify the connection properties and feature class names to correspond to your data.

How to use:
  1. Modify the code to fit your data.
  2. Paste the code into your VBA or VB Application.
Public Sub ConvertSDE2SDE()
  Dim pInWsName As IWorkspaceName
  Dim pInPropSet As IPropertySet
  Set pInPropSet = New PropertySet
  With pInPropSet
    .SetProperty "SERVER", "cuillin"
    .SetProperty "INSTANCE", "cuillin_ora"
    .SetProperty "DATABASE", ""
    .SetProperty "USER", "jim"
    .SetProperty "PASSWORD", "jim"
    .SetProperty "VERSION", "SDE.DEFAULT"
  End With
  Set pInWsName = New WorkspaceName
  pInWsName.WorkspaceFactoryProgID = "esricore.SDEWorkspaceFactory"
  pInWsName.ConnectionProperties = pInPropSet
  Dim pOutWsName As IWorkspaceName
  Dim pOutPropSet As IPropertySet
  Set pOutPropSet = New PropertySet
  With pOutPropSet
    .SetProperty "SERVER", "kintail"
    .SetProperty "INSTANCE", "kintail_sql"
    .SetProperty "DATABASE", "geo"
    .SetProperty "USER", "don"
    .SetProperty "PASSWORD", "don"
    .SetProperty "VERSION", "SDE.DEFAULT"
  End With
  Set pOutWsName = New WorkspaceName
  pOutWsName.WorkspaceFactoryProgID = "esricore.SDEWorkspaceFactory"
  pOutWsName.ConnectionProperties = pOutPropSet

  Dim pInFCName As IFeatureClassName
  Set pInFCName = New FeatureClassName
  Dim pDatasetName As IDatasetName
  Set pDatasetName = pInFCName
  pDatasetName.Name = "Counties"
  Set pDatasetName.WorkspaceName = pInWsName

  Dim sOutFCName As String
  sOutFCName = pDatasetName.Name
  Call ConvertFeatureClass(pInFCName, pOutWsName, Nothing, sOutFCName)

  MsgBox "Conversion completed"
End Sub

Public Sub ConvertFeatureClass(pInFCName As IFeatureClassName, _
                               pOutWorkspaceName As IWorkspaceName, _
                               pOutFDName As IFeatureDatasetName, _
                               strOutFCName As String)
  ' Set Output feature class name
  Dim pOutFCName As IFeatureClassName
  Set pOutFCName = New FeatureClassName
  Dim pDatasetName As IDatasetName
  Set pDatasetName = pOutFCName
  pDatasetName.Name = strOutFCName
  Set pDatasetName.WorkspaceName = pOutWorkspaceName
  If Not pOutFDName Is Nothing Then
    Set pOutFCName.FeatureDatasetName = pOutFDName
  End If
  ' Get the fields for the input feature class
  ' and run them through the field checker
  ' Strip field names which are qualified as Owner.TableName.FieldName
  ' this will avoid breaking the field name width limit
  Dim pFields As IFields
  Dim pOutFields As IFields
  Dim pInFeatureClass As IFeatureClass
  Dim pName As IName
  Set pName = pInFCName
  Set pInFeatureClass = pName.Open
  Dim pSource As IClone
  Set pSource = pInFeatureClass.Fields
  Set pFields = pSource.Clone

  Dim pSQLSyntax As ISQLSyntax
  Set pDatasetName = pInFCName
  Set pName = pDatasetName.WorkspaceName
  Set pSQLSyntax = pName.Open    'Inline QI on Workspace object
  Dim i As Integer
  Dim pField As IField
  Dim pFieldEdit As IFieldEdit
  Dim sStrippedName As String
  Dim sDbName As String, sOwnerName As String, sTableName As String
  ' For each field, strip the name
  ' The check on Owner name avoids processing the Shape.Area and Shape.Len fields.
  ' (these are handled by the feature data converter).
  For i = 0 To pFields.FieldCount - 1
    Set pField = pFields.Field(i)
    Debug.Print pField.Name
    sOwnerName = ""
    pSQLSyntax.ParseColumnName pField.Name, sDbName, sOwnerName, sTableName, sStrippedName
    If Len(sOwnerName) > 0 Then
      Set pFieldEdit = pField
      pFieldEdit.Name = sStrippedName
    End If
    Debug.Print pField.Name
  Next i
  Dim pEnumFieldError As IEnumFieldError
  Dim pFieldChecker As IFieldChecker
  Set pFieldChecker = New FieldChecker
  pFieldChecker.Validate pFields, pEnumFieldError, pOutFields
  Dim pFeatureDataConverter As IFeatureDataConverter
  Set pFeatureDataConverter = New FeatureDataConverter
  ' Do the conversion
  Dim pEnumInvalidObject As IEnumInvalidObject
  Set pEnumInvalidObject = pFeatureDataConverter.ConvertFeatureClass( _
                                   pInFCName, Nothing, _
                                   pOutFDName, pOutFCName, Nothing, _
                                   pOutFields, "", 100, 0)
  ' Report any conversion errors to user
  Dim pInvalidObject As IInvalidObjectInfo
  Set pInvalidObject = pEnumInvalidObject.Next
  If Not pInvalidObject Is Nothing Then
    Dim lObjectCount As Long
    lObjectCount = 0
    Dim sError As String
    sError = pInvalidObject.ErrorDescription
    If pOutWorkspaceName.Type <> esriFileSystemWorkspace Then
      sError = "ObjectID " & pInvalidObject.InvalidObjectID & " - " & sError
    End If
    Do Until pInvalidObject Is Nothing
      lObjectCount = lObjectCount + 1
      Debug.Print "Invalid Object:" & pInvalidObject.InvalidObjectID & ":" & pInvalidObject.ErrorDescription
      Set pInvalidObject = pEnumInvalidObject.Next
    MsgBox lObjectCount & " features were not converted." & vbNewLine & _
           "First error: " & sError
  End If
End Sub