How To: Access a FeatureClass in SDE with IWorkspaceName


Below is a code sample showing how to access a FeatureClass residing in SDE using IFeatureClassName.



  1. Start ArcMap.
  2. Create a new UIButtonControl: How To: Create a new UIButtonControl​
    For more information on creating a UIControl, see the ArcGIS Desktop Help topic: 'Creating custom commands with VBA and UI Controls'
  3. Right-click the UIButtonControl and select View Source.
  4. Copy the following code into the UIButtonControl's click event.
    ''This is how to Use the IWorkspaceName interface to get access to an SDE
    ''WorkSpace and a dataset residing within it...
    ''Users should change the following variable**************
        ''This is the name of the FeatureClass...
        Dim StrFeatureClassName As String
        StrFeatureClassName = "sde.SDE.roads"
        ''This is the connection information that is used to access SDE...
        Dim ppropset As IPropertySet2
        Set ppropset = New PropertySet
        ppropset.SetProperty "SERVER", "Brazil"
        ppropset.SetProperty "INSTANCE", "5151"
        ppropset.SetProperty "USER", "sde"
        ppropset.SetProperty "PASSWORD", "sde"
        ppropset.SetProperty "VERSION", "SDE.DEFAULT"
        Dim pWorkSpaceName2 As IWorkspaceName2
        Set pWorkSpaceName2 = New WorkspaceName
        pWorkSpaceName2.ConnectionProperties = ppropset
        ''Notice this is a String. The ArcObjects help should have a listing of the
        ''other types (featureclass, tables, etc etc)
        pWorkSpaceName2.WorkspaceFactoryProgID = "esriCore.SdeWorkspaceFactory"
        Dim pname As IName
        Set pname = pWorkSpaceName2
        Dim pFeatureWorkspace As IFeatureWorkspace
        Set pFeatureWorkspace = pname.Open
        Dim pfeatureclass As IFeatureClass
        '''Look at the IFeatureWorkSpace as you can open Tables, DataSets, etc etc...
        '''Below we use IFeatureWorkSpace::OpenFeatureClass to get a FeatureClass...
        Set pfeatureclass = pFeatureWorkspace.OpenFeatureClass(StrFeatureClassName)
        ''Proving you have a featureclass...
        MsgBox pfeatureclass.FeatureCount(Nothing)