Joining data追加的字段来自一个或多个表。ArcGIS提供加入数据的几种方法,这取决于数据的来源和数据的基数。本主题介绍了可用的选项,以及如何决定哪个选项是最好的应用程序。
// 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));
}
}
// Make the new TableQueryName.
IQueryName2 queryName2 = (IQueryName2)new TableQueryNameClass();
queryName2.QueryDef = queryDef;
queryName2.PrimaryKey = "streets.StreetID";
queryName2.CopyLocally = true;
// Set the workspace and name of the new QueryTable.
IDatasetName datasetName = (IDatasetName)queryName2;
datasetName.WorkspaceName = workspaceName;
datasetName.Name = "StreetsJoin";
// Open the virtual table.
IName name = (IName)queryName2;
ITable table = (ITable)name.Open();
// Build a memory relationship class.
Type memRelClassFactoryType = Type.GetTypeFromProgID("esriGeodatabase.MemoryRelationshipClassFactory");
IMemoryRelationshipClassFactory memRelClassFactory = (IMemoryRelationshipClassFactory)Activator.CreateInstance(memRelClassFactoryType);
IRelationshipClass relationshipClass = memRelClassFactory.Open("ParcelsOwners", parcelsFeatureClass,
"PARCEL_ID", (IObjectClass)ownersTable, "PARCEL_ID", "Is Owned By", "Owns", esriRelCardinality.esriRelCardinalityOneToOne);
// Open the RelQueryTable as a feature class.
Type rqtFactoryType = Type.GetTypeFromProgID("esriGeodatabase.RelQueryTableFactory");
IRelQueryTableFactory rqtFactory = (IRelQueryTableFactory)Activator.CreateInstance(rqtFactoryType);
ITable relQueryTable = (ITable)rqtFactory.Open(relationshipClass, false, null, null, String.Empty, false, false);