AE+C# SQL属性查询
2012-12-27 13:51
288 查看
此功能是通过点击主窗体上的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;
,触发其点击事件,函数如下所示
//判断是否已经打开了地图
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;
相关文章推荐
- AE高效属性查询接口IWorkspace.ExecuteSQL
- C#+AE 实现点击查询属性功能
- AE属性查询(c#)
- c#+AE-属性查询之框选查询
- C#+AE:如何用TreeList控件实现属性查询
- C#+AE 实现点击查询属性功能
- C#+AE 对于做属性查询的一定总结
- 查询两个表的字段属性差异的sql语句
- SQL字段类型对应C#字符属性类型
- 【SQL语句】 - Ctrl+3 查询表属性的存储过程 [sp_select_talberowName]
- ms sql server 查询一个表的字段属性和主键
- 在C#下使用sql语句(查询,插入,更新,删除……)
- SQL之列名属性为关键字时如何查询
- ms sql server 查询一个表的字段属性和主键
- 在C#中建立复杂的、灵活的SQL查询/命令
- 查询两个表的字段属性差异的sql语句
- 一个简单实用的sql数据库查询工具(c#)
- C#中使用SQL语句生成DataTable,对不同的数据库,处理方式不同,返回的结果属性不同
- C#连接SQL多条件复杂查询
- AE+C#开发问题之三——ISymbologyControlEvents_OnStyleClassChangedEvent的e属性强制转换问题