您的位置:首页 > 数据库

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;

                       
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: