用ArcEngine的接口做過查詢統計的同學應該知道,由於ArcEngine並沒有完整支持SQL,所以做簡單的查詢還好,稍微複雜一點就開始捉襟見肘了。
現在總結一下幾種查詢的方法:
1.IQueryFilter、ISpatialFilter接口。
這是最常見的,把參數設置進去,然後調用IFeatureClass或者ITable的Search方法就行。可以轉爲IQueryFilterDefinition接口設置Order By和Group By語句。
2.IQueryDef接口。
個人覺得這個接口最方便,可以設置多表查詢。雖然有些幫助文檔說不能使用Order By和Group By語句,但是其實是可以的,比如可以這樣設置:
queryDef.Tables = “tableA Group by Field01”;
queryDef.SubFields = “Field01”;
或者
queryDef.Tables = “tableA”;
queryDef.SubFields = “Field01, COUNT(Field01)”;
queryDef.WhereClause = “Field01 is not null GROUP BY Field01 HAVING COUNT(Field01) > 1”;
但是這個接口只可以用在GeoDatabase,不能用於ShapeFile和Coverage數據。
3.使用IDataStatistics接口,然後調用Statistics方法返回IStatisticsResults對象,可以獲取六種統計結果。
4.另外還要加一種方法,就是IWorkspace.ExecuteSQL,可以直接輸入SQL語句,但是這個方法沒有返回值,不能獲取結果,所以只有在做一些系統維護功能,需要直接操縱SDE表的時候纔用到。
暫時只有這些,有遺漏的話以後再補充。
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