How to open a network dataset (ArcObjects .NET 10.6 SDK)
ArcObjects Help for .NET developers > Developing with ArcGIS > Learning ArcObjects > Managing data > Working with feature data > Network datasets > How to open a network dataset (ArcObjects .NET 10.6 SDK)

How to open a network dataset


Opening a network dataset

Opening a network dataset consists of the following steps:
  1. Get the IWorkspace. To learn how to open a workspace, see Connecting to geodatabases and databases.
  2. Using the workspace from step 1, use one of the following options to get the IDatasetContainer:
    1. For FileSystemWorkspaces, such as Smart Data Compression (SDC) and shapefile, get the workspace extension manager first, then use the extension manager to get the workspace extension. The workspace extension can be cast as a dataset container. This is different from local and remote databases, because file workspaces do not have feature datasets.
    2. For LocalDatabaseWorkspaces—such as personal geodatabases and file geodatabase—and for RemoteDatabaseWorkspaces—such as SDE—get the feature dataset container, and use it to get the feature dataset extension. The feature dataset extension can be cast as a dataset container.
This step is shown in the following code sample:
[C#]
// Get the IDatasetContainer based on the workspace type.
ESRI.ArcGIS.Geodatabase.IDatasetContainer3 datasetContainer3=null;

switch (networkDatasetWorkspace.Type)
{
    // Example: FileSystemWorkspaces include SDC and shapefile.
    case ESRI.ArcGIS.Geodatabase.esriWorkspaceType.esriFileSystemWorkspace:
        var workspaceExtensionManager=networkDatasetWorkspace as
            ESRI.ArcGIS.Geodatabase.IWorkspaceExtensionManager;
        ESRI.ArcGIS.esriSystem.UID networkID=new ESRI.ArcGIS.esriSystem.UIDClass();
        networkID.Value="esriGeoDatabase.NetworkDatasetWorkspaceExtension";
        ESRI.ArcGIS.Geodatabase.IWorkspaceExtension workspaceExtension =
            workspaceExtensionManager.FindExtension(networkID);
        datasetContainer3=workspaceExtension as
            ESRI.ArcGIS.Geodatabase.IDatasetContainer3;
        break;

        // Example: LocalDatabaseWorkspaces can be personal geodatabases and file geodatabases.
        // An example RemoteDatabaseWorkspace is an ArcSDE geodatabase.
        // Both LocalDatabaseWorkspace and RemoteDatabaseWorkspace network datasets are 
        // opened the same way.
    case ESRI.ArcGIS.Geodatabase.esriWorkspaceType.esriLocalDatabaseWorkspace:
    case ESRI.ArcGIS.Geodatabase.esriWorkspaceType.esriRemoteDatabaseWorkspace:
        var featureWorkspace=networkDatasetWorkspace as
            ESRI.ArcGIS.Geodatabase.IFeatureWorkspace;
        ESRI.ArcGIS.Geodatabase.IFeatureDataset featureDataset =
            featureWorkspace.OpenFeatureDataset(featureDatasetName);
        var featureDatasetExtensionContainer=featureDataset as
            ESRI.ArcGIS.Geodatabase.IFeatureDatasetExtensionContainer;
        ESRI.ArcGIS.Geodatabase.IFeatureDatasetExtension featureDatasetExtension =
            featureDatasetExtensionContainer.FindExtension
            (ESRI.ArcGIS.Geodatabase.esriDatasetType.esriDTNetworkDataset);
        datasetContainer3=featureDatasetExtension as
            ESRI.ArcGIS.Geodatabase.IDatasetContainer3;
        break;
}

if (datasetContainer3 == null)
    return null;
[VB.NET]
' Get the IDatasetContainer based on the workspace type.
Dim datasetContainer3 As ESRI.ArcGIS.Geodatabase.IDatasetContainer3=Nothing
Select Case networkDatasetWorkspace.Type
    ' Example: FileSystemWorkspaces include SDC and shapefile.
    Case ESRI.ArcGIS.Geodatabase.esriWorkspaceType.esriFileSystemWorkspace
        Dim workspaceExtensionManager=TryCast(networkDatasetWorkspace, ESRI.ArcGIS.Geodatabase.IWorkspaceExtensionManager)
        Dim networkID As ESRI.ArcGIS.esriSystem.UID=New ESRI.ArcGIS.esriSystem.UIDClass()
        networkID.Value="esriGeoDatabase.NetworkDatasetWorkspaceExtension"
        Dim workspaceExtension As ESRI.ArcGIS.Geodatabase.IWorkspaceExtension=workspaceExtensionManager.FindExtension(networkID)
        datasetContainer3=TryCast(workspaceExtension, ESRI.ArcGIS.Geodatabase.IDatasetContainer3)
        Exit Select
        
        ' Example: LocalDatabaseWorkspaces can be personal geodatabases and file geodatabases.
        ' An example RemoteDatabaseWorkspace is an ArcSDE geodatabase.
        ' Both LocalDatabaseWorkspace and RemoteDatabaseWorkspace network datasets are
        ' opened the same way.
    Case ESRI.ArcGIS.Geodatabase.esriWorkspaceType.esriLocalDatabaseWorkspace, ESRI.ArcGIS.Geodatabase.esriWorkspaceType.esriRemoteDatabaseWorkspace
        Dim featureWorkspace=TryCast(networkDatasetWorkspace, ESRI.ArcGIS.Geodatabase.IFeatureWorkspace)
        Dim featureDataset As ESRI.ArcGIS.Geodatabase.IFeatureDataset=featureWorkspace.OpenFeatureDataset(featureDatasetName)
        Dim featureDatasetExtensionContainer=TryCast(featureDataset, ESRI.ArcGIS.Geodatabase.IFeatureDatasetExtensionContainer)
        Dim featureDatasetExtension As ESRI.ArcGIS.Geodatabase.IFeatureDatasetExtension=featureDatasetExtensionContainer.FindExtension(ESRI.ArcGIS.Geodatabase.esriDatasetType.esriDTNetworkDataset)
        datasetContainer3=TryCast(featureDatasetExtension, ESRI.ArcGIS.Geodatabase.IDatasetContainer3)
        Exit Select
End Select
If datasetContainer3 Is Nothing Then
    Return Nothing
End If
  1. Open the network dataset using the dataset container as shown in the following code sample:
[C#]
// Use the container to open the network dataset.
ESRI.ArcGIS.Geodatabase.IDataset dataset=datasetContainer3.get_DatasetByName
    (ESRI.ArcGIS.Geodatabase.esriDatasetType.esriDTNetworkDataset,
    networkDatasetName);
var networkDataset=dataset as ESRI.ArcGIS.Geodatabase.INetworkDataset;
[VB.NET]
' Use the container to open the network dataset.
Dim dataset As ESRI.ArcGIS.Geodatabase.IDataset=datasetContainer3.get_DatasetByName(ESRI.ArcGIS.Geodatabase.esriDatasetType.esriDTNetworkDataset, networkDatasetName)
Dim networkDataset=TryCast(dataset, ESRI.ArcGIS.Geodatabase.INetworkDataset)
4. Optionally, get the data element from the network dataset as shown in the following code sample:
[C#]
// Some methods, such as INASolver.Bind, require an IDENetworkDataset.
// You can access the DataElement from the network dataset via the IDatasetComponent interface.
var datasetComponent=networkDataset as ESRI.ArcGIS.Geodatabase.IDatasetComponent;
var deNetworkDataset=datasetComponent.DataElement as
    ESRI.ArcGIS.Geodatabase.IDENetworkDataset;
[VB.NET]
' Some methods, such as INASolver.Bind, require an IDENetworkDataset.
' You can access the DataElement from the network dataset via the IDatasetComponent interface.
Dim datasetComponent=TryCast(networkDataset, ESRI.ArcGIS.Geodatabase.IDatasetComponent)
Dim deNetworkDataset=TryCast(datasetComponent.DataElement, ESRI.ArcGIS.Geodatabase.IDENetworkDataset)
The following method is composed of the previous code fragments:
[C#]
public ESRI.ArcGIS.Geodatabase.INetworkDataset OpenNetworkDataset
    (ESRI.ArcGIS.Geodatabase.IWorkspace networkDatasetWorkspace, System.String
    networkDatasetName, System.String featureDatasetName)
{
    // Shapefile and SDC network datasets will not have a feature dataset, 
    // so pass in the featureDatasetName as an empty string for those types.
    if (networkDatasetWorkspace == null || networkDatasetName == "" ||
        featureDatasetName == null)
        return null;

    // Get the IDatasetContainer based on the workspace type.
    ESRI.ArcGIS.Geodatabase.IDatasetContainer3 datasetContainer3=null;
    switch (networkDatasetWorkspace.Type)
    {
        // Example: FileSystemWorkspaces include SDC and shapefile.
        case ESRI.ArcGIS.Geodatabase.esriWorkspaceType.esriFileSystemWorkspace:
            var workspaceExtensionManager=networkDatasetWorkspace as
                ESRI.ArcGIS.Geodatabase.IWorkspaceExtensionManager;
            ESRI.ArcGIS.esriSystem.UID networkID=new
                ESRI.ArcGIS.esriSystem.UIDClass();
            networkID.Value="esriGeoDatabase.NetworkDatasetWorkspaceExtension";
            ESRI.ArcGIS.Geodatabase.IWorkspaceExtension workspaceExtension =
                workspaceExtensionManager.FindExtension(networkID);
            datasetContainer3=workspaceExtension as
                ESRI.ArcGIS.Geodatabase.IDatasetContainer3;
            break;

            // Example: LocalDatabaseWorkspaces can be personal geodatabases and file geodatabases.
            // An example RemoteDatabaseWorkspace is an ArcSDE geodatabase.
            // Both LocalDatabaseWorkspace and RemoteDatabaseWorkspace network datasets are 
            // opened the same way.
        case ESRI.ArcGIS.Geodatabase.esriWorkspaceType.esriLocalDatabaseWorkspace:
        case ESRI.ArcGIS.Geodatabase.esriWorkspaceType.esriRemoteDatabaseWorkspace:
            var featureWorkspace=networkDatasetWorkspace as
                ESRI.ArcGIS.Geodatabase.IFeatureWorkspace;
            ESRI.ArcGIS.Geodatabase.IFeatureDataset featureDataset =
                featureWorkspace.OpenFeatureDataset(featureDatasetName);
            var featureDatasetExtensionContainer=featureDataset as
                ESRI.ArcGIS.Geodatabase.IFeatureDatasetExtensionContainer;
            ESRI.ArcGIS.Geodatabase.IFeatureDatasetExtension featureDatasetExtension
               =featureDatasetExtensionContainer.FindExtension
                (ESRI.ArcGIS.Geodatabase.esriDatasetType.esriDTNetworkDataset);
            datasetContainer3=featureDatasetExtension as
                ESRI.ArcGIS.Geodatabase.IDatasetContainer3;
            break;
    }

    if (datasetContainer3 == null)
        return null;

    // Use the container to open the network dataset.
    ESRI.ArcGIS.Geodatabase.IDataset dataset=datasetContainer3.get_DatasetByName
        (ESRI.ArcGIS.Geodatabase.esriDatasetType.esriDTNetworkDataset,
        networkDatasetName);
    var networkDataset=dataset as ESRI.ArcGIS.Geodatabase.INetworkDataset;

    // Some methods, such as INASolver.Bind, require an IDENetworkDataset.
    // You can access the DataElement from the network dataset via the IDatasetComponent interface
    var datasetComponent=networkDataset as
        ESRI.ArcGIS.Geodatabase.IDatasetComponent;
    var deNetworkDataset=datasetComponent.DataElement as
        ESRI.ArcGIS.Geodatabase.IDENetworkDataset;

    return networkDataset;
}
[VB.NET]
Public Function OpenNetworkDataset(ByVal networkDatasetWorkspace As ESRI.ArcGIS.Geodatabase.IWorkspace, ByVal networkDatasetName As System.String, ByVal featureDatasetName As System.String) As ESRI.ArcGIS.Geodatabase.INetworkDataset
    ' Shapefile and SDC network datasets will not have a feature dataset,
    ' so pass in the featureDatasetName as an empty string for those types.
    If networkDatasetWorkspace Is Nothing OrElse networkDatasetName="" OrElse featureDatasetName Is Nothing Then
        Return Nothing
    End If
    
    ' Get the IDatasetContainer based on the workspace type.
    Dim datasetContainer3 As ESRI.ArcGIS.Geodatabase.IDatasetContainer3=Nothing
    Select Case networkDatasetWorkspace.Type
        ' Example: FileSystemWorkspaces include SDC and shapefile.
        Case ESRI.ArcGIS.Geodatabase.esriWorkspaceType.esriFileSystemWorkspace
            Dim workspaceExtensionManager=TryCast(networkDatasetWorkspace, ESRI.ArcGIS.Geodatabase.IWorkspaceExtensionManager)
            Dim networkID As ESRI.ArcGIS.esriSystem.UID=New ESRI.ArcGIS.esriSystem.UIDClass()
            networkID.Value="esriGeoDatabase.NetworkDatasetWorkspaceExtension"
            Dim workspaceExtension As ESRI.ArcGIS.Geodatabase.IWorkspaceExtension=workspaceExtensionManager.FindExtension(networkID)
            datasetContainer3=TryCast(workspaceExtension, ESRI.ArcGIS.Geodatabase.IDatasetContainer3)
            Exit Select
            
            ' Example: LocalDatabaseWorkspaces can be personal geodatabases and file geodatabases.
            ' An example RemoteDatabaseWorkspace is an ArcSDE geodatabase.
            ' Both LocalDatabaseWorkspace and RemoteDatabaseWorkspace network datasets are
            ' opened the same way.
        Case ESRI.ArcGIS.Geodatabase.esriWorkspaceType.esriLocalDatabaseWorkspace, ESRI.ArcGIS.Geodatabase.esriWorkspaceType.esriRemoteDatabaseWorkspace
            Dim featureWorkspace=TryCast(networkDatasetWorkspace, ESRI.ArcGIS.Geodatabase.IFeatureWorkspace)
            Dim featureDataset As ESRI.ArcGIS.Geodatabase.IFeatureDataset=featureWorkspace.OpenFeatureDataset(featureDatasetName)
            Dim featureDatasetExtensionContainer=TryCast(featureDataset, ESRI.ArcGIS.Geodatabase.IFeatureDatasetExtensionContainer)
            Dim featureDatasetExtension As ESRI.ArcGIS.Geodatabase.IFeatureDatasetExtension=featureDatasetExtensionContainer.FindExtension(ESRI.ArcGIS.Geodatabase.esriDatasetType.esriDTNetworkDataset)
            datasetContainer3=TryCast(featureDatasetExtension, ESRI.ArcGIS.Geodatabase.IDatasetContainer3)
            Exit Select
    End Select
    
    If datasetContainer3 Is Nothing Then
        Return Nothing
    End If
    
    ' Use the container to open the network dataset.
    Dim dataset As ESRI.ArcGIS.Geodatabase.IDataset=datasetContainer3.get_DatasetByName(ESRI.ArcGIS.Geodatabase.esriDatasetType.esriDTNetworkDataset, networkDatasetName)
    Dim networkDataset=TryCast(dataset, ESRI.ArcGIS.Geodatabase.INetworkDataset)
    
    ' Some methods, such as INASolver.Bind, require an IDENetworkDataset.
    ' You can access the DataElement from the network dataset via the IDatasetComponent interface
    Dim datasetComponent=TryCast(networkDataset, ESRI.ArcGIS.Geodatabase.IDatasetComponent)
    Dim deNetworkDataset=TryCast(datasetComponent.DataElement, ESRI.ArcGIS.Geodatabase.IDENetworkDataset)
    
    Return networkDataset
End Function


See Also:

What is ArcGIS Network Analyst extension?
What is a network dataset?
Geodatabase
NetworkAnalyst
About the ArcGIS Network Analyst extension Tutorial




To use the code in this topic, reference the following assemblies in your Visual Studio project. In the code files, you will need using (C#) or Imports (VB .NET) directives for the corresponding namespaces (given in parenthesis below if different from the assembly name):
Development licensing Deployment licensing
Engine Developer Kit Engine: Network Analyst
ArcGIS Desktop Advanced: Network Analyst ArcGIS Desktop Advanced: Network Analyst
ArcGIS Desktop Standard: Network Analyst ArcGIS Desktop Standard: Network Analyst
ArcGIS Desktop Basic: Network Analyst ArcGIS Desktop Basic: Network Analyst