How to query a data source with the Common Data Source API


Summary This topic demonstrates how to use the generic Common Data Source API interfaces in the Web ADF to query a feature layer within a map resource.

The sample assumes that a MapResourceManager and Map (named Map1) are available in the current scope and that a layer in a map resource has the following properties: 1) an id value of "states", 2) a field named STATE_NAME which contains attributes that start with 'N', and 3) the same attributes have spatial features within the geographic extent (-120, 30) to (-100, 50) in decimal degrees. Note that the map resource type is non-specific, therefore any data source implementation that is part of the Web ADF and supports queries can be used (e.g. ArcIMS, ArcGIS Server). An initial test for query support is included in the sample. The results for the Query method are returned in a standard .NET DataTable object (System.Data.DataTable).
[C#]
foreach (ESRI.ArcGIS.ADF.Web.DataSources.IGISFunctionality gisFunctionality in
    Map1.GetFunctionalities())
{
    ESRI.ArcGIS.ADF.Web.DataSources.IGISResource gisResource =
        gisFunctionality.Resource;
    bool supported = gisResource.SupportsFunctionality(typeof
        (ESRI.ArcGIS.ADF.Web.DataSources.IQueryFunctionality));

    if (supported)
    {
        ESRI.ArcGIS.ADF.Web.DataSources.IQueryFunctionality queryFunctionality = 
            (ESRI.ArcGIS.ADF.Web.DataSources.IQueryFunctionality)
            gisResource.CreateFunctionality(typeof
            (ESRI.ArcGIS.ADF.Web.DataSources.IQueryFunctionality), null);

        string[] layerIDs = null;
        string[] layerNames = null;
        queryFunctionality.GetQueryableLayers(null, out layerIDs, out layerNames);

        ESRI.ArcGIS.ADF.Web.SpatialFilter spatialFilter = new
            ESRI.ArcGIS.ADF.Web.SpatialFilter();
        spatialFilter.ReturnADFGeometries = true;
        spatialFilter.MaxRecords = 1000;

        spatialFilter.WhereClause = "STATE_NAME LIKE 'N%'";

        ESRI.ArcGIS.ADF.Web.Geometry.Envelope adfEnvelope = new
            ESRI.ArcGIS.ADF.Web.Geometry.Envelope( - 120, 30,  - 100, 50);
        spatialFilter.Geometry = adfEnvelope;

        string activeLayerString = "states";
        System.Data.DataTable queryResultsDataTable = null;
        for (int i = 0; i < layerNames.Length; i++)
        {
            if (layerNames[i].Equals(activeLayerString))
            {
                queryResultsDataTable = queryFunctionality.Query
                    (gisFunctionality.Name, layerIDs[i], spatialFilter);
                break;
            }
        }
    }
}
[VB.NET]
For Each gisFunctionality As ESRI.ArcGIS.ADF.Web.DataSources.IGISFunctionality In Map1.GetFunctionalities()
    Dim gisResource As ESRI.ArcGIS.ADF.Web.DataSources.IGISResource = gisFunctionality.Resource
    Dim supported As Boolean = gisResource.SupportsFunctionality(GetType(ESRI.ArcGIS.ADF.Web.DataSources.IQueryFunctionality))
    
    If supported Then
        Dim queryFunctionality As ESRI.ArcGIS.ADF.Web.DataSources.IQueryFunctionality = CType(gisResource.CreateFunctionality (GetType(ESRI.ArcGIS.ADF.Web.DataSources.IQueryFunctionality), Nothing), ESRI.ArcGIS.ADF.Web.DataSources.IQueryFunctionality)
        
        Dim layerIDs As String() = Nothing
        Dim layerNames As String() = Nothing
        queryFunctionality.GetQueryableLayers(Nothing, layerIDs, layerNames)
        
        Dim spatialFilter As ESRI.ArcGIS.ADF.Web.SpatialFilter = New ESRI.ArcGIS.ADF.Web.SpatialFilter()
        spatialFilter.ReturnADFGeometries = True
        spatialFilter.MaxRecords = 1000
        
        spatialFilter.WhereClause = "STATE_NAME LIKE 'N%'"
        
        Dim adfEnvelope As ESRI.ArcGIS.ADF.Web.Geometry.Envelope = New ESRI.ArcGIS.ADF.Web.Geometry.Envelope( -120, 30, -100, 50)
        spatialFilter.Geometry = adfEnvelope
        
        Dim activeLayerString As String = "states"
        Dim queryResultsDataTable As System.Data.DataTable = Nothing
        Dim i As Integer = 0
        Do While i < layerNames.Length
            If layerNames(i).Equals(activeLayerString) Then
                queryResultsDataTable = queryFunctionality.Query(gisFunctionality.Name, layerIDs(i), spatialFilter)
                Exit Do
            End If
            i + = 1
        Loop
    End If
Next gisFunctionality