【AE】多表的联合查询

时间:2022-01-08 00:26:30

多表的联合查询

// Create the query definition.
IQueryDef queryDef = featureWorkspace.CreateQueryDef();

// Provide a list of tables to join.
queryDef.Tables = "streets, altname";

// Set the subfields and the WhereClause (in this case, the join condition).
queryDef.SubFields = "streets.NAME, streets.TYPE, altname.ST_NAME, altname.ST_TYPE";
queryDef.WhereClause = "streets.OBJECTID = altname.JOINID";

// Get a cursor of the results and find the indexes of the fields to display.
using(ComReleaser comReleaser = new ComReleaser())
{
    ICursor cursor = queryDef.Evaluate();
    comReleaser.ManageLifetime(cursor);
    int streetsNameIndex = cursor.FindField("streets.NAME");
    int streetsTypeIndex = cursor.FindField("streets.TYPE");
    int altnameNameIndex = cursor.FindField("altname.ST_NAME");
    int altnameTypeIndex = cursor.FindField("altname.ST_TYPE");

    // Use the cursor to step through the results, displaying the names and altnames of each 
    // street.
    IRow row = null;
    while ((row = cursor.NextRow()) != null)
    {
        Console.WriteLine("Street name: {0} {1}. - Alt. name: {2} {3}.",
            row.get_Value(streetsNameIndex), row.get_Value(streetsTypeIndex),
            row.get_Value(altnameNameIndex), row.get_Value(altnameTypeIndex));
    }
}