Geodatabase应用程序编程接口(API)提供了许多不同的方式来查询表和要素类。本主题介绍如何使用query filters, spatial filters, and QueryDefs对数据进行查询。
1.IQueryFilter interface
// Create the query filter.
IQueryFilter queryFilter = new QueryFilterClass();
// Select the fields to be returned—the name and address of the businesses.
queryFilter.SubFields = "NAME, ADDRESS";
// Set the filter to return only restaurants.
queryFilter.WhereClause = "TYPE = 'Restaurant'";
// Use the PostfixClause to alphabetically order the set by name.
IQueryFilterDefinition queryFilterDef = (IQueryFilterDefinition)queryFilter;
queryFilterDef.PostfixClause = "ORDER BY NAME";
// Output the returned names and addresses.
int nameIndex = table.FindField("NAME");
int addressIndex = table.FindField("ADDRESS");
using (ComReleaser comReleaser = new ComReleaser())
{
ICursor cursor = table.Search(queryFilter, true);
comReleaser.ManageLifetime(cursor);
IRow row = null;
while ((row = cursor.NextRow()) != null)
{
String name = Convert.ToString(row.get_Value(nameIndex));
String address = Convert.ToString(row.get_Value(addressIndex));
Console.WriteLine("{0} - {1}", name, address);
}
}
2.ISpatialFilter interface
// Create the envelope and define its position.
IEnvelope envelope = new EnvelopeClass();
envelope.PutCoords(-84.4078, 33.7787, -84.3856, 33.7997);
// Create the spatial filter and set its spatial constraints.
ISpatialFilter spatialFilter = new SpatialFilterClass
{
Geometry = envelope,
GeometryField = featureClass.ShapeFieldName,
SpatialRel = esriSpatialRelEnum.esriSpatialRelIntersects
};
// Set the attribute constraints and subfields.
// You want to exclude ramps, highways and interstates.
spatialFilter.WhereClause = "NAME <> 'Ramp' AND PRE_TYPE NOT IN ('Hwy', 'I')";
spatialFilter.SubFields = "NAME, TYPE";
// Execute the query, displaying the roads returned.
int nameIndex = featureClass.FindField("NAME");
int typeIndex = featureClass.FindField("TYPE");
using (ComReleaser comReleaser = new ComReleaser())
{
IFeatureCursor featureCursor = featureClass.Search(spatialFilter, true);
comReleaser.ManageLifetime(featureCursor);
IFeature feature = null;
while ((feature = featureCursor.NextFeature()) != null)
{
String roadName = Convert.ToString(feature.get_Value(nameIndex));
String roadType = Convert.ToString(feature.get_Value(typeIndex));
Console.WriteLine("Name: {0}, Type: {1}", roadName, roadType);
}
}
//if a geometry bag is being used as the filter's query geometry, create a spatial index for the geometry bag before being assigned //to the geometry property. This can drastically increase the query's efficiency. The following code example shows how to do this:
// Cast the bag to the ISpatialIndex interface.
ISpatialIndex spatialIndex = (ISpatialIndex)geometryBag;
spatialIndex.AllowIndexing = true;
spatialIndex.Invalidate();
3.IQueryDef interface
// Create the QueryDef.
IQueryDef2 queryDef2 = (IQueryDef2)featureWorkspace.CreateQueryDef();
// Specify the table and fields to query.
queryDef2.Tables = "Cities";
queryDef2.SubFields = "Name, Pop1996";
queryDef2.PostfixClause = "ORDER BY Pop1996 DESC";
// Execute the query.
using (ComReleaser comReleaser = new ComReleaser())
{
ICursor cursor = queryDef2.Evaluate2(true);
comReleaser.ManageLifetime(cursor);
int nameIndex = cursor.FindField("Name");
int pop1996Index = cursor.FindField("Pop1996");
IRow row = null;
while ((row = cursor.NextRow()) != null)
{
String cityName = Convert.ToString(row.get_Value(nameIndex));
int population = Convert.ToInt32(row.get_Value(pop1996Index));
Console.WriteLine("{0}: {1}", cityName, population);
}
}
通过创建游标 QueryDef 可用于生成一个虚拟表或要素类。
// Create a reference to a TableQueryName object.
IQueryName2 queryName2 = new TableQueryNameClass();
queryName2.PrimaryKey = String.Empty;
// Specify the query definition.
queryName2.QueryDef = queryDef;
// Get a name object for the workspace.
IDataset dataset = (IDataset)workspace;
IWorkspaceName workspaceName = (IWorkspaceName)dataset.FullName;
// Cast the TableQueryName object to the IDatasetName interface and open it.
IDatasetName datasetName = (IDatasetName)queryName2;
datasetName.WorkspaceName = workspaceName;
datasetName.Name = tableName;
IName name = (IName)datasetName;
// Open the name object and get a reference to a table object.
ITable table = (ITable)name.Open();
4.WHERE clauses