此功能是通过点击主窗体上的SQL查询按钮,触发其点击事件,函数如下所示
//判断是否已经打开了地图
pMap = axMapControl1.Map;
if (pMap.LayerCount < 1)
{
MessageBox.Show("请先加载图层!");
return;
}
//创建一个SelectLayer类来控制选择图层查询
SelectLayer lSelectLayer = new SelectLayer(axMapControl1);
//打开查询对话框
lSelectLayer.ShowDialog();
SelectLayer类是一个窗体类,其样式如下
SelectLayer类代码如下:
using System;
using System.Data;
using System.Windows.Forms;
using System.IO;
using ESRI.ArcGIS.DataSourcesFile;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Controls;
using ESRI.ArcGIS.MapControl;
using ESRI.ArcGIS.Geometry;
using ESRI.ArcGIS.DataSourcesRaster;
using ESRI.ArcGIS.CartoUI;
using DevComponents.DotNetBar;
using System.Collections;
namespace linxiangtu
{
public partial class SelectLayer : Office2007Form
{
public SelectLayer(AxMapControl lAxMapControl)
{
InitializeComponent();
//传入可供查询的地图
this.pAxMapControl = lAxMapControl;
//将所有图层名添加进comboBoxLayer,并设置默认值为第一项
for (int i = 0; i < pAxMapControl .LayerCount ; i++)
{
comboBoxLayer.Items.Add(pAxMapControl .get_Layer (i).Name );
}
comboBoxLayer.SelectedIndex = 0;
//设置comboBoxMethod的选择项,并设置默认值为第一项
comboBoxMethod.Items.Add("新建选择集");
comboBoxMethod.Items.Add("添加进已有的选择集");
comboBoxMethod.Items.Add("从当前选择集中清除");
comboBoxMethod.Items.Add("从当前选择集中再次筛选");
comboBoxMethod.SelectedIndex = 0;
//最初时获取唯一值框不可使用
listBox_Value.Visible = false;
}
//全局变量pAxMapControl
private AxMapControl pAxMapControl;
#region SymbolButton 运算符点击事件
private void button_equal_Click(object sender, EventArgs e)
{
textBox_WhereClause.Text += " =";
}
private void button_Notequal_Click(object sender, EventArgs e)
{
textBox_WhereClause.Text += " <>";
}
private void button_Big_Click(object sender, EventArgs e)
{
textBox_WhereClause.Text += " >";
}
private void button_BigEqual_Click(object sender, EventArgs e)
{
textBox_WhereClause.Text += " >=";
}
private void button_Small_Click(object sender, EventArgs e)
{
textBox_WhereClause.Text += " <";
}
private void button_Smallequal_Click(object sender, EventArgs e)
{
textBox_WhereClause.Text += " <=";
}
private void button_Brace_Click(object sender, EventArgs e)
{
textBox_WhereClause.Text += " ()";
}
private void button_Like_Click(object sender, EventArgs e)
{
textBox_WhereClause.Text += " LIKE";
}
private void button_And_Click(object sender, EventArgs e)
{
textBox_WhereClause.Text += " AND";
}
private void button_Or_Click(object sender, EventArgs e)
{
textBox_WhereClause.Text += " OR";
}
private void button_Not_Click(object sender, EventArgs e)
{
textBox_WhereClause.Text += " NOT";
}
private void button_IS_Click(object sender, EventArgs e)
{
textBox_WhereClause.Text += " IS";
}
private void button_Clear_Click(object sender, EventArgs e)
{
textBox_WhereClause.Text = "";
}
private void button_What_Click(object sender, EventArgs e)
{
textBox_WhereClause.Text += " ?";
}
private void button_All_Click(object sender, EventArgs e)
{
textBox_WhereClause.Text += " *";
}
#endregion
private void comboBoxLayer_SelectedValueChanged(object sender, EventArgs e)
{
//如果没有图层被选择,将不能添加字段
if (comboBoxLayer.Text != null)
{
//清空listBox_Field
listBox_Field.Items.Clear();
//获取要素
IFeatureLayer lFeatureLayer = (IFeatureLayer)pAxMapControl.Map.get_Layer(comboBoxLayer.SelectedIndex);
IFeatureCursor lFeatureCursor = lFeatureLayer.Search(null, true);
IFeature lFeature = lFeatureCursor.NextFeature();
//循环添加所属图层的字段名进入listBox_Field中
//对于esriFieldTypeGeometry类型的自动则不予以添加
for (int i = 0; i < lFeature.Fields.FieldCount; i++)
{
if (lFeature.Fields.get_Field(i).Type != esriFieldType.esriFieldTypeGeometry)
{
listBox_Field.Items.Add(lFeature.Fields.get_Field(i).Name);
}
}
//设置当前选择字段为第一个
listBox_Field.SelectedIndex = 0;
//将描述信息修改
label_Description.Text = "SELECT * FROM " + comboBoxLayer.Text + " WHERE:";
}
}
//双击选中字段,将其添加进条件框
private void listBox_Field_DoubleClick(object sender, EventArgs e)
{
IFeatureLayer lFeatureLayer = (IFeatureLayer)pAxMapControl.Map.get_Layer(comboBoxLayer.SelectedIndex);
if (lFeatureLayer.DataSourceType == "Shapefile Feature Class")//shapefile文件
{
textBox_WhereClause.Text += " "" + listBox_Field.Text + "" ";
}
else
{
textBox_WhereClause.Text += " [" + listBox_Field.Text + "] ";
}
}
//双击选中值,将其添加进条件框
private void listBox_Value_DoubleClick(object sender, EventArgs e)
{
textBox_WhereClause.Text += " " + listBox_Value.Text ;
}
//当选择另一个字段时,将List_Value框隐藏
private void listBox_Field_SelectedIndexChanged(object sender, EventArgs e)
{
listBox_Value.Visible = false;
button_GetValue.Enabled = true;
}
//应用按钮
private void button_Apply_Click_1(object sender, EventArgs e)
{
if (textBox_WhereClause.Text != "")
{
try
{
IFeatureLayer lFeatureLayer = (IFeatureLayer)pAxMapControl.get_Layer(comboBoxLayer.SelectedIndex);
IFeatureSelection lFeatureSelection = (IFeatureSelection)lFeatureLayer;
//判断选择的SQL方法的类型
esriSelectionResultEnum lesriSREnum = esriSelectionResultEnum.esriSelectionResultNew;
switch (comboBoxMethod.SelectedIndex)
{
case 0:
lesriSREnum = esriSelectionResultEnum.esriSelectionResultNew;
break;
case 1:
lesriSREnum = esriSelectionResultEnum.esriSelectionResultAdd;
break;
case 2:
lesriSREnum = esriSelectionResultEnum.esriSelectionResultSubtract;
break;
case 3:
lesriSREnum = esriSelectionResultEnum.esriSelectionResultAnd;
break;
default:
MessageBox.Show("请选择一种查询方法");
break;
}
//创建查询的条件
IQueryFilter2 lQueryFilter = new QueryFilterClass();
lQueryFilter.WhereClause = textBox_WhereClause.Text;
//根据查询添加进行选择,并刷新屏幕
lFeatureSelection.SelectFeatures(lQueryFilter, lesriSREnum, false);
pAxMapControl.ActiveView.Refresh();
}
catch (Exception)
{
MessageBox.Show("输入的SQL查询不符合语法要求");
}
}
else
{
MessageBox.Show("请选择需要查询的图层");
}
}
//取消按钮
private void button_Cancel_Click_1(object sender, EventArgs e)
{
this.Close();
}
//清除按钮
private void button_Clear_Click_1(object sender, EventArgs e)
{
textBox_WhereClause.Clear();
}
//获取唯一值按钮
private void button_GetValue_Click(object sender, EventArgs e)
{
//获取要素图层与要素类,将其作为参数传入GetUniqueValue()函数
IFeatureLayer lFeatureLayer = (IFeatureLayer)pAxMapControl.get_Layer(comboBoxLayer.SelectedIndex);
IFeatureClass lFeatureClass = lFeatureLayer.FeatureClass;
//将返回的所有值存入allValue数组中,并进行排序
string[] allValue = GetUniqueValue(lFeatureClass ,listBox_Field.Text);
Array.Sort(allValue);
//获取字段对象,用于在将其值添加进listbox_value中时判断字段类型
IFeatureCursor lFeatureCursor = lFeatureLayer.Search(null, true);
IFeature lFeature = lFeatureCursor.NextFeature();
IField lField=new FieldClass();
for (int j = 0; j < lFeature.Fields.FieldCount; j++)
{
if (listBox_Field.Text == lFeature.Fields.get_Field(j).Name)
{
lField = lFeature.Fields.get_Field(j);
}
}
//将之前listBox_Value中的值清空,然后添加此次选中字段的所有数据
listBox_Value.Items.Clear();
for (int i = 0; i < allValue.Length; i++)
{
if (lField.Type==esriFieldType.esriFieldTypeString)
{
allValue[i]="\'" + allValue[i] + "\'";
listBox_Value.Items.Add(allValue[i]);
}
else
{
listBox_Value.Items.Add(allValue[i]);
}
}
listBox_Value.Visible = true;
button_GetValue.Enabled = false;
}
/// <summary>
/// 得到要素类某字段的唯一值
/// </summary>
/// <param name="pFeatureClass">要素类</param>
/// <param name="strFld">指定要得到唯一值的字段</param>
/// <returns>唯一值字符数据</returns>
public string[] GetUniqueValue(IFeatureClass pFeatureClass,string strFld)
{
//得到IFeatureCursor游标
IFeatureCursor pCursor=pFeatureClass.Search(null,false);
//IDataStatistics对象实例生成
IDataStatistics pData=new DataStatisticsClass();
pData.Field=strFld;
pData.Cursor=pCursor as ICursor;
//枚举唯一值
IEnumerator pEnumVar=pData.UniqueValues ;
//记录总数
int RecordCount=pData.UniqueValueCount;
//字符数组
string[] strValue=new string[RecordCount];
pEnumVar.Reset();
int i=0;
while(pEnumVar.MoveNext())
{
strValue[i++]=pEnumVar.Current.ToString();
}
return strValue;
}
}
}