原创企业级控件库之组合查询控件

原创企业级控件库之组合查询控件

发布日期:2010年12月10日星期五 作者:EricHu

  无论对于企业还是对于软件开发者来说,拥有自己的一套常用控件库,对于开发产品来说不仅可以缩短开发时间,提高开发效率,同时对一个企业整个产品的形象也会大大提高。本系列控件为作者在实际开发应用中总结出来,且成功应用于多个项目。现对整个控件库一一讲解,最后我会把整个控件库开源,方便你的使用,同时会给一个综合应用这些控件的实例。

  成就别人、等于成就自己。我没什么要求,欢迎大家多多支持与评论,觉得不错的,记得点击文章左下角的”关注博客”,就这么简单。

  整个控件样式如下:

原创企业级控件库之组合查询控件

  说明:

  1、本控件分为组合查询与固定查询两种,上图为组合查询。固定查询见下图。

  2、组合查询以下几部份组成:

a. 查询项:设置查询的项目,通俗点就是表中的字段。

b. 运算符:设置查询条件,如:等于、大小、包含、为空等。

c. 值:设置查询的值。

d. 组合方式:当查询条件大于两个时,其组合条件可设为:与方式、或方式、非方式三种。

e. 新增:新增查询条件到组合条件框。

f. 清除:当输入错误时,可以清除组合条件框中的组合查询条件。

g. 查询:当用户单击查询时,返回查询表达式,供用户使用。

h. 固定查询:当用户单击固定查询时,控件变成固定查询控件,同时,固定查询变成“组合查询”,如下图:  

原创企业级控件库之组合查询控件

  3、本控件特点:

a. 对用户输入的危险字符进行了屏蔽,可有效防止对数据库的破坏。

b. 控件返回的查询条件都是合法的Sql语句中的Where条件表达式,可以直接使用。

c. 开放源代码,用户可以根据自己的需要进行定制,消除你的后顾之优。

  4、本控件类图如下所示:

原创企业级控件库之组合查询控件

5、本控件核心代码

a. 得到组合查询表达式

  	#region 组合查询表达式
          /// <summary>
        /// 单击[查询]按钮时发生
          /// </summary>
        [Category("组合查询"), Description("单击[查询]按钮时发生。"), Browsable(true)]
        public event EventHandler OnQueryClicked;

        private string _queryExpression;
        /// <summary>
        /// 最终的查询表达式,可直接用于Where子句中
          /// </summary>
        [Category("组合查询"), Description("最终的查询表达式,可直接用于Where子句中。"), Browsable(false)]
        public string QueryExpression
        {
            get { return _queryExpression; }
            set
            {
                _queryExpression = value;
                if (OnQueryClicked != null)
                {
                    OnQueryClicked(this, null);
                }
            }
        }
 	#endregion

b. 设置查询项中要显示的数据列表

        #region 设置查询项中要显示的数据列表
         /// <summary>
        /// 设置查询项中要显示的数据列表(推荐使用这个方法)
        /// </summary>
        /// <param name="dicListQueryItems">表示键和值的集合(键:数据字段,值:数据字段对应的数据类型)</param>
        public void SetQueryItems(Dictionary<string, string> dicListQueryItems)
        {
            cboQueryItems.Items.Clear();
            dicQueryItem = null;
            dicQueryItem = dicListQueryItems;
            foreach (KeyValuePair<string, string> kvp in dicListQueryItems)
            {
                cboQueryItems.Items.Add(kvp.Key);
            }

            if (cboQueryItems.Items.Contains("案卷号"))//把案卷号显示在第一个
            {
                cboQueryItems.Items.Remove("案卷号");
                cboQueryItems.Items.Insert(0, "案卷号");
            }
            cboQueryItems.SelectedIndex = 0;
        }

        /// <summary>
        /// 设置查询项中要显示的数据列表
        /// </summary>
        /// <param name="sQueryItems">string类型数组</param>
        public void SetQueryItems(string[] sQueryItems)
        {
            cboQueryItems.Items.Clear();

            foreach (string queryItem in sQueryItems)
            {
                cboQueryItems.Items.Add(queryItem);
            }
            cboQueryItems.SelectedIndex = 0;
        }

        /// <summary>
        /// 设置查询项中要显示的数据列表
        /// </summary>
        /// <param name="listQueryItems">List泛型</param>
        public void SetQueryItems(List<string> listQueryItems)
        {
            cboQueryItems.Items.Clear();
            foreach (string queryItem in listQueryItems)
            {
                cboQueryItems.Items.Add(queryItem);
            }
            cboQueryItems.SelectedIndex = 0;
        }       
        #endregion


C.增加查询条件

代码 

        #region 增加查询条件单击事件 btnAddQueryCondition_Click(object sender, EventArgs e)

        private void btnAddQueryCondition_Click(object sender, EventArgs e)
        {
            if (cboQueryItems.Items.Count == 0)
            {
                DialogHelper.ShowErrorMsg("查询项为空,不能进行查询!");
                return;
            }
            else
            {
                string sQueryItem = cboQueryItems.Text.Trim(); //要查询的项
                string sQueryValue = txtQueryValue.Text.Trim(); //查询项的值
                string sCombinMode = string.Empty; //组合方式
                string sQueryExpress = string.Empty;//查询条件表达式

                if (cboOperator.Text != "为空" && cboOperator.Text != "不为空")
                {
                    if (string.IsNullOrEmpty(txtQueryValue.Text.Trim()))
                    {
                        DialogHelper.ShowWarningMsg("必须输入查询项的值!");
                        txtQueryValue.Focus();
                        return;
                    }
                    else
                    {
                        if (StringHelper.HasDangerousWord(txtQueryValue.Text.Trim())) 
                        {
                            DialogHelper.ShowWarningMsg("对不起,你的输入含有危险字符,请重新输入!");
                            txtQueryValue.Clear();
                            txtQueryValue.Focus();
                            return;
                        }
                    }
                }

                switch (cboCombinMode.Text)
                {
                    case "与方式":
                        sCombinMode = "AND";
                        break;
                    case "或方式":
                        sCombinMode = "OR";
                        break;
                    case "非方式":
                        sCombinMode = "AND NOT";
                        break;
                    default:
                        break;
                }
                #region 条件设置
                switch (cboOperator.Text)
                {
                    case "包含":
                        sQueryExpress = sQueryItem + " LIKE '%" + sQueryValue + "%'";
                        break;
                    case "左包含":
                        sQueryExpress = sQueryItem + " LIKE '" + sQueryValue + "%'";
                        break;
                    case "右包含":
                        sQueryExpress = sQueryItem + " LIKE '%" + sQueryValue + "'";
                        break;
                    case "为空":
                        sQueryExpress = sQueryItem + " IS NULL OR " + sQueryItem + "= ''";
                        break;
                    case "不为空":
                        sQueryExpress = sQueryItem + " IS NOT NULL And " + sQueryItem + "!= ''";
                        break;
                    case "大于":
                        if (dicQueryItem.Count == 0)
                        {
                            sQueryExpress = sQueryItem + " >  " + sQueryValue;
                        }
                        else
                        {
                            foreach (KeyValuePair<string, string> kvp in dicQueryItem)
                            {
                                if (cboQueryItems.Text == kvp.Key)
                                {
                                    if ((kvp.Value == "System.DateTime" || kvp.Value == "System.String")
                                        && kvp.Key != "案卷号") //kvp.Key != "案卷号"主是要因为案卷号是字符型,案卷号>'90',得不到正确结果,所以在此这样做
                                    {
                                        sQueryExpress = sQueryItem + " >  '" + sQueryValue + "'";
                                    }
                                    else
                                    {
                                        sQueryExpress = sQueryItem + " >  " + sQueryValue;
                                    }
                                }
                            }
                        }
                        break;
                    case "大于或等于":
                        if (dicQueryItem.Count == 0)
                        {
                            sQueryExpress = sQueryItem + " >=  " + sQueryValue;
                        }
                        else
                        {
                            foreach (KeyValuePair<string, string> kvp in dicQueryItem)
                            {
                                if (cboQueryItems.Text == kvp.Key)
                                {
                                    if ((kvp.Value == "System.DateTime" || kvp.Value == "System.String") && kvp.Key != "案卷号")
                                    {
                                        sQueryExpress = sQueryItem + " >=  '" + sQueryValue + "'";
                                    }
                                    else
                                    {
                                        sQueryExpress = sQueryItem + " >=  " + sQueryValue;
                                    }
                                }
                            }
                        }
                        break;
                    case "等于":
                        if (dicQueryItem.Count == 0)
                        {
                            sQueryExpress = sQueryItem + " =  " + sQueryValue;
                        }
                        else
                        {
                            foreach (KeyValuePair<string, string> kvp in dicQueryItem)
                            {
                                if (cboQueryItems.Text == kvp.Key)
                                {
                                    //---3
                                    if (kvp.Value == "System.DateTime" || kvp.Value == "System.String")
                                    {
                                        sQueryExpress = sQueryItem + " =  '" + sQueryValue + "'";
                                    }
                                    else
                                    {
                                        sQueryExpress = sQueryItem + " =  " + sQueryValue;
                                    }

                                }
                            }
                        }
                        break;
                    case "小于或等于":
                        if (dicQueryItem.Count == 0)
                        {
                            sQueryExpress = sQueryItem + " <=  " + sQueryValue;
                        }
                        else
                        {
                            foreach (KeyValuePair<string, string> kvp in dicQueryItem)
                            {
                                if (cboQueryItems.Text == kvp.Key)
                                {
                                    if ((kvp.Value == "System.DateTime" || kvp.Value == "System.String") && kvp.Key != "案卷号")
                                    {
                                        sQueryExpress = sQueryItem + " <=  '" + sQueryValue + "'";
                                    }
                                    else
                                    {
                                        sQueryExpress = sQueryItem + " <=  " + sQueryValue;
                                    }
                                }
                            }
                        }
                        break;
                    case "小于":
                        if (dicQueryItem.Count == 0)
                        {
                            sQueryExpress = sQueryItem + " <  " + sQueryValue;
                        }
                        else
                        {
                            foreach (KeyValuePair<string, string> kvp in dicQueryItem)
                            {
                                if (cboQueryItems.Text == kvp.Key)
                                {
                                    if ((kvp.Value == "System.DateTime" || kvp.Value == "System.String") && kvp.Key != "案卷号")
                                    {
                                        sQueryExpress = sQueryItem + " <  '" + sQueryValue + "'";
                                    }
                                    else
                                    {
                                        sQueryExpress = sQueryItem + " <  " + sQueryValue;
                                    }
                                }
                            }
                        }
                        break;
                    default:
                        break;
                }

                if (!string.IsNullOrEmpty(txtQueryCondition.Text.Trim()))
                {
                    sQueryExpress = sCombinMode + " " + sQueryExpress;
                }               

                txtQueryCondition.AppendText(" " + sQueryExpress);
                #endregion
            }
        }
        #endregion


6、控件编译后,你可把编译后的dll文件直接拖运到Microsoft Visual Studi工具箱中,就可以看到我们的组合查询控件了。在实际应用中,仅需以下两个步骤即可完成整个代码。

a.在窗体社会分配加载时绑定相应的查询项

代码 

        /// <summary>
        /// 增加测试数据
        /// </summary>
        private void BindTestData()
        {
            dtTest = new DataTable();
            dtTest.Columns.Add(new DataColumn("身份证号", typeof(System.String)));
            dtTest.Columns.Add(new DataColumn("姓名", typeof(System.String)));
            dtTest.Columns.Add(new DataColumn("QQ", typeof(System.String)));
            dtTest.Columns.Add(new DataColumn("年龄", typeof(System.Int16)));
            dtTest.Columns.Add(new DataColumn("出生时间", typeof(System.DateTime)));
        }

        private void FrmUcCombinQueryTest_Shown(object sender, EventArgs e)
        {
            BindTestData();

            //绑定查询项
            Dictionary<string, string> dicListQueryItems = new Dictionary<string, string>();
            foreach (DataColumn dc in dtTest.Columns)
            {
                dicListQueryItems.Add(dc.ColumnName, dc.DataType.ToString());
            }
            ucCombinQueryTest.SetQueryItems(dicListQueryItems);
        }

b.单击查询时,返回给用户的查询表达式,用户直接得到使用,在组合查询的OnQueryClicked事件代码中进行处理。

  
  
<!-- Code highlighting produced by Actipro CodeHighlighter (freeware) http://www.CodeHighlighter.com/ --> private void ucCombinQueryTest_OnQueryClicked( object sender, EventArgs e) { // 得到查询表达式 MessageBox.Show(ucCombinQueryTest.QueryExpression); }

7、下面给出组合查询控件完整代码

代码 

#region  版权信息
/*---------------------------------------------------------------------*
// Copyright (C) 2008 http://www.cnblogs.com/huyong
// 版权所有。 
// 项目  名称:《Winform通用控件库》
// 文  件  名: UcCombinQuery.cs
// 类  全  名: DotNet.Controls.UcCombinQuery 
// 描      述:  组合查询控件
// 创建  时间: 2008-08-05
// 创建人信息: [**** 姓名:胡勇 QQ:80368704 E-Mail:[email protected] *****]
*----------------------------------------------------------------------*/
#endregion

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using DotNet.Common;

namespace DotNet.Controls
{
    /// <summary>
    /// 组合查询控件
    /// UcCombinQuery
    /// 修改纪录
    ///     2010-12-6  胡勇 优化相关代码。
    ///     2010-11-29 胡勇 取消对:清除组合查询条件时的提示。
    ///     2010-11-8  胡勇 要是查询项中包含"案卷号",则把案卷号移动到第一个查询项。
    ///     2010-11-5  胡勇 对案卷号的查询,不加单引号。
    ///     2010-8-30  胡勇 对查询字符型字段进行>、<、>=、<=、=操作时,其值自动加单引号。
    ///     2008-08-05 胡勇 创建组合查询控件
    /// <author>
    ///     <name>胡勇</name>
    ///     <QQ>80368704</QQ>
    ///     <Email>[email protected]</Email>
    /// </author>
    /// </summary>
    [ToolboxItem(true)]
    [DefaultEvent("OnQueryClicked")]
    [ToolboxBitmap(typeof(UcCombinQuery), "DotNet.Controls.Images.UcCombinQuery.bmp")]
    [Description("组合查询控件")]
    public partial class UcCombinQuery : UserControl
    { 
        #region 公共变量
        Dictionary<string, string> dicQueryItem = new Dictionary<string, string>();
        #endregion

        #region 公共方法
        /// <summary>
        /// 查询模式
        /// </summary>
        public enum QueryMode
        {
            /// <summary>
            /// 固定查询
            /// </summary>
            FixQueryMode       = 0x0001,
            /// <summary>
            /// 组合查询
            /// </summary>
            CompositeQueryMode = 0x0002
        }

        /// <summary>
        /// 设置查询模式
        /// </summary>
        /// <param name="queryMode">查询模式</param>
        public void SetQueryMode(QueryMode queryMode)
        {
            if (queryMode == QueryMode.FixQueryMode)
            {
                btnQueryMode.Text = "固定查询";
            }
            else
            {
                btnQueryMode.Text = "组合查询";
            }
            btnQueryMode_Click(null, null);
        }
        #endregion

        #region 可见性属性
        private bool _QueryModeButtomVisible = true;
        /// <summary>
        /// 组合查询按钮是否可见
        /// </summary>
        [Category("组合查询"), Description("组合查询是否可见。")]
        public bool QueryModeButtomVisible
        {
            get
            {
                return _QueryModeButtomVisible;
            }
            set
            {
                _QueryModeButtomVisible = value;
                if (!_QueryModeButtomVisible)
                {
                    toolStripSeparator1.Visible = false;
                }
                this.btnQueryMode.Visible = _QueryModeButtomVisible;
            }
        }
        #endregion

        #region 构造函数
        public UcCombinQuery()
        {
            InitializeComponent();
        }
        #endregion

        #region 组合查询表达式
        /// <summary>
        /// 单击[查询]按钮时发生
        /// </summary>
        [Category("组合查询"), Description("单击[查询]按钮时发生。"), Browsable(true)]
        public event EventHandler OnQueryClicked;

        private string _queryExpression;
        /// <summary>
        /// 最终的查询表达式,可直接用于Where子句中
        /// </summary>
        [Category("组合查询"), Description("最终的查询表达式,可直接用于Where子句中。"), Browsable(false)]
        public string QueryExpression
        {
            get { return _queryExpression; }
            set
            {
                _queryExpression = value;
                if (OnQueryClicked != null)
                {
                    OnQueryClicked(this, null);
                }
            }
        }
        #endregion

        #region 查询项相关控制

        #region 设置查询项中要显示的数据列表
        /// <summary>
        /// 设置查询项中要显示的数据列表(推荐使用这个方法)
        /// </summary>
        /// <param name="dicListQueryItems">表示键和值的集合(键:数据字段,值:数据字段对应的数据类型)</param>
        public void SetQueryItems(Dictionary<string, string> dicListQueryItems)
        {
            cboQueryItems.Items.Clear();
            dicQueryItem = null;
            dicQueryItem = dicListQueryItems;
            foreach (KeyValuePair<string, string> kvp in dicListQueryItems)
            {
                cboQueryItems.Items.Add(kvp.Key);
            }

            if (cboQueryItems.Items.Contains("案卷号"))//把案卷号显示在第一个
            {
                cboQueryItems.Items.Remove("案卷号");
                cboQueryItems.Items.Insert(0, "案卷号");
            }
            cboQueryItems.SelectedIndex = 0;
        }

        /// <summary>
        /// 设置查询项中要显示的数据列表
        /// </summary>
        /// <param name="sQueryItems">string类型数组</param>
        public void SetQueryItems(string[] sQueryItems)
        {
            cboQueryItems.Items.Clear();

            foreach (string queryItem in sQueryItems)
            {
                cboQueryItems.Items.Add(queryItem);
            }
            cboQueryItems.SelectedIndex = 0;
        }

        /// <summary>
        /// 设置查询项中要显示的数据列表
        /// </summary>
        /// <param name="listQueryItems">List泛型</param>
        public void SetQueryItems(List<string> listQueryItems)
        {
            cboQueryItems.Items.Clear();
            foreach (string queryItem in listQueryItems)
            {
                cboQueryItems.Items.Add(queryItem);
            }
            cboQueryItems.SelectedIndex = 0;
        }       
        #endregion

        /// <summary>
        /// 设置查询项的选择索引项
        /// </summary>
        /// <param name="index">索引的下标</param>
        public void SetQueryItemsSelectIndex(int index)
        {
            this.cboQueryItems.SelectedIndex = index;        
        }

        /// <summary>
        /// 设置查询项的选择内容
        /// </summary>
        /// <param name="sTxt">查询项选中的内容</param>
        public void SetQueryItemSelectText(string sTxt)
        {
            this.cboQueryItems.SelectedText =sTxt;
        }

        /// <summary>
        /// 清空选项值
        /// </summary>
        public void ClearQueryItems()
        {
            cboQueryItems.Items.Clear();
        }
        #endregion

        #region 事件代码

        #region 窗体Load事件 UcCombinQuery_Load(object sender, EventArgs e)
        private void UcCombinQuery_Load(object sender, EventArgs e)
        {
            this.Height = 25;
        }
        #endregion

        #region 设置查询模式(组合查询或固定查询:btnQueryMode_Click(object sender, EventArgs e)
        private void btnQueryMode_Click(object sender, EventArgs e)
        {
            cboQueryItems.Focus();
            //spContainerHorizontal.Panel2Collapsed = !spContainerHorizontal.Panel2Collapsed;
            lblOperator.Visible = !lblOperator.Visible;
            cboOperator.Visible = !cboOperator.Visible;
            lblCombinMode.Visible = !lblCombinMode.Visible;
            cboCombinMode.Visible = !cboCombinMode.Visible;
            sp1.Visible = !sp1.Visible;
            if (btnQueryMode.Text == "固定查询")
            {
                btnQueryMode.Text = "组合查询";
                btnAddQueryCondition.Visible = !btnAddQueryCondition.Visible;
                btnClsQueryCondition.Visible = !btnClsQueryCondition.Visible;
                this.Height = 25;
                btnQueryMode.Image = DotNet.Controls.Properties.Resources.组合查询;//导入图片
            }
            else if (btnQueryMode.Text == "组合查询")
            {
                txtQueryCondition.Clear();
                cboOperator.SelectedIndex = 0;
                cboCombinMode.SelectedIndex = 0;
                btnAddQueryCondition.Visible = !btnAddQueryCondition.Visible;
                btnClsQueryCondition.Visible = !btnClsQueryCondition.Visible;
                this.Height = 86;
                btnQueryMode.Text = "固定查询";
                btnQueryMode.Image = DotNet.Controls.Properties.Resources.固定查询;
            }
        }
        #endregion

        #region 增加查询条件单击事件 btnAddQueryCondition_Click(object sender, EventArgs e)

        private void btnAddQueryCondition_Click(object sender, EventArgs e)
        {
            if (cboQueryItems.Items.Count == 0)
            {
                DialogHelper.ShowErrorMsg("查询项为空,不能进行查询!");
                return;
            }
            else
            {
                string sQueryItem = cboQueryItems.Text.Trim(); //要查询的项
                string sQueryValue = txtQueryValue.Text.Trim(); //查询项的值
                string sCombinMode = string.Empty; //组合方式
                string sQueryExpress = string.Empty;//查询条件表达式

                if (cboOperator.Text != "为空" && cboOperator.Text != "不为空")
                {
                    if (string.IsNullOrEmpty(txtQueryValue.Text.Trim()))
                    {
                        DialogHelper.ShowWarningMsg("必须输入查询项的值!");
                        txtQueryValue.Focus();
                        return;
                    }
                    else
                    {
                        if (StringHelper.HasDangerousWord(txtQueryValue.Text.Trim())) 
                        {
                            DialogHelper.ShowWarningMsg("对不起,你的输入含有危险字符,请重新输入!");
                            txtQueryValue.Clear();
                            txtQueryValue.Focus();
                            return;
                        }
                    }
                }

                switch (cboCombinMode.Text)
                {
                    case "与方式":
                        sCombinMode = "AND";
                        break;
                    case "或方式":
                        sCombinMode = "OR";
                        break;
                    case "非方式":
                        sCombinMode = "AND NOT";
                        break;
                    default:
                        break;
                }
                #region 条件设置
                switch (cboOperator.Text)
                {
                    case "包含":
                        sQueryExpress = sQueryItem + " LIKE '%" + sQueryValue + "%'";
                        break;
                    case "左包含":
                        sQueryExpress = sQueryItem + " LIKE '" + sQueryValue + "%'";
                        break;
                    case "右包含":
                        sQueryExpress = sQueryItem + " LIKE '%" + sQueryValue + "'";
                        break;
                    case "为空":
                        sQueryExpress = sQueryItem + " IS NULL OR " + sQueryItem + "= ''";
                        break;
                    case "不为空":
                        sQueryExpress = sQueryItem + " IS NOT NULL And " + sQueryItem + "!= ''";
                        break;
                    case "大于":
                        if (dicQueryItem.Count == 0)
                        {
                            sQueryExpress = sQueryItem + " >  " + sQueryValue;
                        }
                        else
                        {
                            foreach (KeyValuePair<string, string> kvp in dicQueryItem)
                            {
                                if (cboQueryItems.Text == kvp.Key)
                                {
                                    if ((kvp.Value == "System.DateTime" || kvp.Value == "System.String")
                                        && kvp.Key != "案卷号") //kvp.Key != "案卷号"主是要因为案卷号是字符型,案卷号>'90',得不到正确结果,所以在此这样做
                                    {
                                        sQueryExpress = sQueryItem + " >  '" + sQueryValue + "'";
                                    }
                                    else
                                    {
                                        sQueryExpress = sQueryItem + " >  " + sQueryValue;
                                    }
                                }
                            }
                        }
                        break;
                    case "大于或等于":
                        if (dicQueryItem.Count == 0)
                        {
                            sQueryExpress = sQueryItem + " >=  " + sQueryValue;
                        }
                        else
                        {
                            foreach (KeyValuePair<string, string> kvp in dicQueryItem)
                            {
                                if (cboQueryItems.Text == kvp.Key)
                                {
                                    if ((kvp.Value == "System.DateTime" || kvp.Value == "System.String") && kvp.Key != "案卷号")
                                    {
                                        sQueryExpress = sQueryItem + " >=  '" + sQueryValue + "'";
                                    }
                                    else
                                    {
                                        sQueryExpress = sQueryItem + " >=  " + sQueryValue;
                                    }
                                }
                            }
                        }
                        break;
                    case "等于":
                        if (dicQueryItem.Count == 0)
                        {
                            sQueryExpress = sQueryItem + " =  " + sQueryValue;
                        }
                        else
                        {
                            foreach (KeyValuePair<string, string> kvp in dicQueryItem)
                            {
                                if (cboQueryItems.Text == kvp.Key)
                                {
                                    //---3
                                    if (kvp.Value == "System.DateTime" || kvp.Value == "System.String")
                                    {
                                        sQueryExpress = sQueryItem + " =  '" + sQueryValue + "'";
                                    }
                                    else
                                    {
                                        sQueryExpress = sQueryItem + " =  " + sQueryValue;
                                    }

                                }
                            }
                        }
                        break;
                    case "小于或等于":
                        if (dicQueryItem.Count == 0)
                        {
                            sQueryExpress = sQueryItem + " <=  " + sQueryValue;
                        }
                        else
                        {
                            foreach (KeyValuePair<string, string> kvp in dicQueryItem)
                            {
                                if (cboQueryItems.Text == kvp.Key)
                                {
                                    if ((kvp.Value == "System.DateTime" || kvp.Value == "System.String") && kvp.Key != "案卷号")
                                    {
                                        sQueryExpress = sQueryItem + " <=  '" + sQueryValue + "'";
                                    }
                                    else
                                    {
                                        sQueryExpress = sQueryItem + " <=  " + sQueryValue;
                                    }
                                }
                            }
                        }
                        break;
                    case "小于":
                        if (dicQueryItem.Count == 0)
                        {
                            sQueryExpress = sQueryItem + " <  " + sQueryValue;
                        }
                        else
                        {
                            foreach (KeyValuePair<string, string> kvp in dicQueryItem)
                            {
                                if (cboQueryItems.Text == kvp.Key)
                                {
                                    if ((kvp.Value == "System.DateTime" || kvp.Value == "System.String") && kvp.Key != "案卷号")
                                    {
                                        sQueryExpress = sQueryItem + " <  '" + sQueryValue + "'";
                                    }
                                    else
                                    {
                                        sQueryExpress = sQueryItem + " <  " + sQueryValue;
                                    }
                                }
                            }
                        }
                        break;
                    default:
                        break;
                }

                if (!string.IsNullOrEmpty(txtQueryCondition.Text.Trim()))
                {
                    sQueryExpress = sCombinMode + " " + sQueryExpress;
                }               

                txtQueryCondition.AppendText(" " + sQueryExpress);
                #endregion
            }
        }
        #endregion

        #region 清除查询条件 btnClsQueryCondition_Click(object sender, EventArgs e)
        //清除查询条件
        private void btnClsQueryCondition_Click(object sender, EventArgs e)
        {
            if (txtQueryCondition.Text.Trim() != string.Empty)
            {
                txtQueryCondition.Clear();
            }
        }
        #endregion

        #region 查询单击事件 btnQuery_Click(object sender, EventArgs e)
        private void btnQuery_Click(object sender, EventArgs e)
        {
            if (cboQueryItems.Items.Count == 0)
            {
                DialogHelper.ShowErrorMsg("查询项为空!");
                return;
            }
            else
            {
                if (btnQueryMode.Text == "组合查询")
                {
                    if (string.IsNullOrEmpty(txtQueryValue.Text.Trim()))
                    {
                        QueryExpression = "1 = 1";
                        return;
                    }
                    else
                    {
                        if (StringHelper.HasDangerousWord(txtQueryValue.Text.Trim())) //危险字符判断
                        {
                            DialogHelper.ShowWarningMsg("对不起,你的输入包含危险字符,请重新输入!");
                            txtQueryValue.Clear();
                            txtQueryValue.Focus();
                            return;
                        }
                        else
                        {
                            if (dicQueryItem.Count == 0)
                            {
                                QueryExpression = cboQueryItems.Text + " = '" + txtQueryValue.Text.Trim() + "'";//精确查询
                            }
                            else
                            {
                                foreach (KeyValuePair<string, string> kvp in dicQueryItem)
                                {
                                    if (cboQueryItems.Text == kvp.Key)
                                    {
                                        if (kvp.Value == "System.DateTime")
                                        {
                                            QueryExpression = cboQueryItems.Text + " =  '" + txtQueryValue.Text.Trim() + "'";//精确查询
                                        }
                                        else
                                        {
                                            QueryExpression = cboQueryItems.Text + " = '" + txtQueryValue.Text.Trim() + "'";//精确查询
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
                else
                {
                    if (string.IsNullOrEmpty(txtQueryCondition.Text.Trim()))
                    {
                        if (cboOperator.Text != "为空" && cboOperator.Text != "不为空")
                        {
                            if (txtQueryValue.Text.Trim() == string.Empty)
                            {
                                DialogHelper.ShowWarningMsg("必须输入查询项的值!");
                                txtQueryValue.Focus();
                                return;
                            }
                            else
                            {
                                if (StringHelper.HasDangerousWord(txtQueryValue.Text.Trim()))
                                {
                                    DialogHelper.ShowWarningMsg("对不起,你的输入含有危险字符,请重新输入!");
                                    txtQueryValue.Clear();
                                    txtQueryValue.Focus();
                                    return;
                                }
                                else
                                {
                                    btnAddQueryCondition_Click(sender, e);
                                }
                            }
                        }
                        else
                        {
                            btnAddQueryCondition_Click(sender, e);
                        }
                    }
                    QueryExpression = txtQueryCondition.Text.Trim();
                }
            }
        }
        #endregion

        #endregion
    }
}

© 2010 EricHu

原创作品,转贴请注明作者和出处,留此信息。

你可能感兴趣的:(查询)