ArcObjects Library Reference  (GeoDatabase)    

IQueryDef Example

[Visual Basic 6.0]
    ' This example creates a QueryDef
    
    'Connect to Geodatabase (SDE)
    Dim pPropset As IPropertySet
    Set pPropset = New PropertySet
    With pPropset
      .SetProperty "Server", "griot"
      .SetProperty "Instance", "5151"
      .SetProperty "Database", ""
      .SetProperty "user", "shell"
      .SetProperty "password", "shell"
      .SetProperty "version", "SDE.DEFAULT"
    End With
        
    Dim pFact As IWorkspaceFactory
    Set pFact = New SdeWorkspaceFactory
    
    ' Open the Feature Dataset
    Dim pWorkspace As IWorkspace
    Set pWorkspace = pFact.Open(pPropset, Me.hWnd)
    
    ' Create Feature Workspace
    Dim pFeatureWorkspace As IFeatureWorkspace
    Set pFeatureWorkspace = pWorkspace
    
    ' Create Query Definition
    Dim pQueryDef As IQueryDef
    Set pQueryDef = pFeatureWorkspace.CreateQueryDef
    
    ' Provide list of tables to join
    pQueryDef.Tables = "datesjoin,dudatest"
    
    ' Retrieve the fields from all  tables
    pQueryDef.SubFields = "sde.datesjoin.dt_field, sde.dudates.dt_field"
    
    ' Set up join
    pQueryDef.WhereClause = "datesjoin.dt_field = dudates.dt_field"
    
    ' Create FeatureDataset. Note the use of .OpenFeatureQuery. The
    ' Name "MyJoin" is the name of the result of the query def
    ' and is used in place of a Feature Class name.
    Dim pFeatureDataset As IFeatureDataset
    Set pFeatureDataset = pFeatureWorkspace.OpenFeatureQuery("MyJoin", pQueryDef)
    
    'Open Layer to test against
    Dim pFeatureClassContainer As IFeatureClassContainer
    Set pFeatureClassContainer = pFeatureDataset
     
    Dim pFeatureClass As IFeatureClass
    Set pFeatureClass = pFeatureClassContainer.ClassByName("MyJoin")

[C#]

    //IQueryDef Example

    public void IQueryDef_Example(IWorkspace workspace)
    {
        IFeatureWorkspace featureWorkspace = (IFeatureWorkspace)workspace;
        //create query definition
        IQueryDef queryDef = featureWorkspace.CreateQueryDef();
        //provide list of tables to join
        queryDef.Tables = "datesjoin,dudatest";
        //retrieve the fields from all tables
        queryDef.SubFields = "sde.datesjoin.dt_field, sde.dudates.dt_field";
        //set up join
        queryDef.WhereClause = "datesjoin.dt_field = dudates.dt_field";

        //Create FeatureDataset. Note the use of .OpenFeatureQuery.
        //The name "MyJoin" is the name of the restult of the query def and
        //is used in place of a feature class name.
        IFeatureDataset featureDataset = featureWorkspace.OpenFeatureQuery("MyJoin", queryDef);
        //open layer to test against
        IFeatureClassContainer featureClassContainer = (IFeatureClassContainer)featureDataset;
        IFeatureClass featureClass = featureClassContainer.get_ClassByName("MyJoin");
    }


[Visual Basic .NET, C++]
No example is available for Visual Basic .NET or C++. To view a Visual Basic 6.0 or C# example, click the Language Filter button Language Filter in the upper-left corner of the page.