How to convert shapefile to an enterprise geodatabase


This script demonstrates how you can use the data conversion functions to convert data from a Shapefile to an ArcSDE Geodatabase.

How to use

  1. Modify the code to fit your data.
  2. Paste the code into your VBA Application.
[VBA]
Sub LoadShps()
    ' +++ Set connection properties. Change the properties to match your
    ' +++ server name, instance, user name and password for your SDE database
    Dim pOutSDEPropset As IPropertySet
    Set pOutSDEPropset = New propertySet
    With pOutSDEPropset
        .SetProperty "Server", "stout"
        .SetProperty "Instance", "sde4_ora"
        .SetProperty "user", "gdb"
        .SetProperty "password", "gdb"
        .SetProperty "version", "SDE.DEFAULT"
    End With
    
    ' +++ Create a new feature datset name object for the output SDE feature dataset, call
    ' +++ it "USA"
    Dim pOutSDEWorkspaceName As IWorkspaceName
    Set pOutSDEWorkspaceName = New WorkspaceName
    pOutSDEWorkspaceName.connectionProperties = pOutSDEPropset
    pOutSDEWorkspaceName.WorkspaceFactoryProgID = "esriDataSourcesGDB.SdeWorkspaceFactory.1"
    
    Dim pOutSDEFeatDSName As IFeatureDatasetName
    Set pOutSDEFeatDSName = New FeatureDatasetName
    
    Dim pSDEDSname As IDatasetName
    Set pSDEDSname = pOutSDEFeatDSName
    Set pSDEDSname.WorkspaceName = pOutSDEWorkspaceName
    pSDEDSname.Name = "USA"
    
    ' +++ Get the name object for the input shapefile workspace
    Dim pInShpWorkspaceName As IWorkspaceName
    Set pInShpWorkspaceName = New WorkspaceName
    pInShpWorkspaceName.PathName = "C:\data\usa"
    pInShpWorkspaceName.WorkspaceFactoryProgID = "esriDataSourcesFile.ShapefileWorkspaceFactory.1"
    
    Dim pInShpFeatCLSNm As IFeatureClassName
    Set pInShpFeatCLSNm = New FeatureClassName
    Dim pShpDatasetName As IDatasetName
    Set pShpDatasetName = pInShpFeatCLSNm
    pShpDatasetName.Name = "counties"
    Set pShpDatasetName.WorkspaceName = pInShpWorkspaceName
    
    ' +++ create the new output FeatureClass name object that will be passed
    ' +++ into the conversion function
    Dim pOutputDSName As IDatasetName
    Dim pOutputFCName As IFeatureClassName
    Set pOutputFCName = New FeatureClassName
    Set pOutputDSName = pOutputFCName
    Dim pInDSNAme As IDatasetName
    
    ' +++ Set the new FeatureClass name to be the same as the input FeatureClass name
    Set pInDSNAme = pInShpFeatCLSNm
    pOutputDSName.Name = "counties"
    ' +++ Open the input Shapefile FeatureClass object, so that we can get its fields
    Dim pName As IName
    Dim pInShpFeatCls As IFeatureClass
    Set pName = pInShpFeatCLSNm
    Set pInShpFeatCls = pName.Open
    
    ' +++ Get the fields for the input feature class and run them through
    ' +++ field checker to make sure there are no illegal or duplicate field names
    Dim pOutSDEFlds As IFields
    Dim pInShpFlds As IFields
    Dim pFldChk As IFieldChecker
    Dim i As Long
    Dim pGeoField As IField
    Dim pOutSDEGeoDef As IGeometryDef
    Dim pOutSDEGeoDefEdit As IGeometryDefEdit
    Set pInShpFlds = pInShpFeatCls.Fields
    Set pFldChk = New FieldChecker
    pFldChk.Validate pInShpFlds, Nothing, pOutSDEFlds
    
    ' +++ Loop through the output fields to find the geometry field
    For i = 0 To pOutSDEFlds.FieldCount
        If pOutSDEFlds.Field(i).Type = esriFieldTypeGeometry Then
            Set pGeoField = pOutSDEFlds.Field(i)
            Exit For
        End If
    Next i
    
    ' +++ Get the geometry field's geometry definition
    Set pOutSDEGeoDef = pGeoField.GeometryDef
    
    ' +++ Give the geometry definition a spatial index grid count and grid size
    Set pOutSDEGeoDefEdit = pOutSDEGeoDef
    pOutSDEGeoDefEdit.GridCount = 1
    pOutSDEGeoDefEdit.GridSize(0) = 1500000
    
    ' +++ Now use IFeatureDataConverter::Convert to create the output FeatureDataset and
    ' +++ FeatureClass.
    Dim pShpToFc As IFeatureDataConverter
    Set pShpToFc = New FeatureDataConverter
    pShpToFc.ConvertFeatureClass pInShpFeatCLSNm, Nothing, pOutSDEFeatDSName, _
        pOutputFCName, Nothing, pOutSDEFlds, "", 1000, 0
    
    MsgBox "Load operation complete!", vbInformation
End Sub