SQL查询条件生成器

本文介绍了SQL查询条件生成器的用途,它主要用于数据库编程中的条件查询。文章列举了生成器的源码,包括IFilter接口及其子接口IFilterString,这些接口定义了用于构建查询条件的属性和方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 在数据库的编程中,常常要对数据库的数据根据条件查询,查询条件生成器正是因此而生。

一、查询条件生成器源码

        查询条件生成器由以下六个文件组成

       1、接口IFilter.cs

  1. using System;
  2. namespace SearchFilter
  3. {
  4.     /// <summary>
  5.     /// IFilter 的摘要说明
  6.     /// 描述: 定义属性
  7.     /// 作者: xlFancy
  8.     /// 日期: 2008.05.10
  9.     /// 版本: 1.0.0.0
  10.     /// </summary>
  11.     public interface IFilter
  12.     {
  13.         string Name { get;}
  14.         string Text { get;}
  15.         DataType DataType { get;}
  16.         string CompareCode { set;get;}
  17.         string LogicalCode { set;get;}
  18.         string BracketCode { set;get;}
  19.         string Value { set;get;}
  20.     }
  21. }

       2、接口IFilterString.cs

  1. using System;
  2. namespace SearchFilter
  3. {
  4.     /// <summary>
  5.     /// IFilterString 的摘要说明
  6.     /// 描述: 定义属性,索引,方法
  7.     /// 作者: xlFancy
  8.     /// 日期: 2008.05.10
  9.     /// 版本: 1.0.0.0
  10.     /// </summary>
  11.     public interface IFilterString
  12.     {
  13.         int FieldCount { get; }
  14.         IFilter this[int Index] { setget; }
  15.         IFilter this[string Key] { setget; }
  16.         /// <summary>
  17.         /// 返回查询条件表达式,能自动删除错误的或不匹配的左括弧、右括弧
  18.         /// </summary>
  19.         string FilterText { get; }
  20.         void FilterClear();
  21.     }
  22. }

       3、数据类型枚举EnumConst.cs

  1. using System;
  2. namespace SearchFilter
  3. {
  4.     /// <summary>
  5.     /// DataType 的摘要说明
  6.     /// 描述: 定义数据类型
  7.     /// 作者: xlFancy
  8.     /// 日期: 2008.05.10
  9.     /// 版本: 1.0.0.0
  10.     /// </summary>
  11.     public enum DataType
  12.     {
  13.         /// <summary>
  14.         /// 布尔类型
  15.         /// </summary>
  16.         Boolen,
  17.         /// <summary>
  18.         /// 日期类型
  19.         /// </summary>
  20.         DataTime,
  21.         /// <summary>
  22.         /// 字符类型
  23.         /// </summary>
  24.         String,
  25.         /// <summary>
  26.         /// 数值类型
  27.         /// </summary>
  28.         Number
  29.     }
  30. }

       4、实现接口IFilter类Filter.cs

  1. using System;
  2. namespace SearchFilter
  3. {
  4.     /// <summary>
  5.     /// Filter 的摘要说明
  6.     /// 功能: 实现IFilter所定义的属性
  7.     /// 作者: xlFancy
  8.     /// 日期: 2008.05.10
  9.     /// 版本: 1.0.0.0
  10.     /// </summary>
  11.     class Filter:IFilter
  12.     {
  13.         private string _name = "";
  14.         private string _text = "";
  15.         private DataType _datatype = DataType.String;
  16.         private string _comparecode = "LIKE";
  17.         private string _logicalcode = "";
  18.         private string _bracketcode = "";
  19.         private string _value = "";
  20.         /// <summary>
  21.         /// 初始化供查询的字段
  22.         /// </summary>
  23.         /// <param name="FieldName">字段名称</param>
  24.         /// <param name="FieldText">字段文本,用于显示</param>
  25.         /// <param name="FieldType">字段类型,只能是DataType枚举值</param>
  26.         public Filter(string FieldName, string FieldText, DataType FieldType)
  27.         {
  28.             _name=FieldName;
  29.             _text=FieldText;
  30.             _datatype=FieldType;
  31.         }
  32.         public string Name { get { return _name; } }
  33.         public string Text { get { return _text; } }
  34.         public DataType DataType { get { return _datatype; } }
  35.         public string CompareCode
  36.         {
  37.             get { return _comparecode; }
  38.             set { _comparecode = value; }
  39.         }
  40.         public string LogicalCode 
  41.         { 
  42.             get { return _logicalcode; }
  43.             set { _logicalcode = value; }
  44.         }
  45.         public string BracketCode 
  46.         {
  47.             get { return _bracketcode; }
  48.             set { _bracketcode = value; }
  49.         }
  50.         public string Value
  51.         {
  52.             get 
  53.             {
  54.                 if (this.CompareCode =="LIKE" || this.CompareCode =="NOT LIKE")
  55.                 {
  56.                     return string.Format("'%{0}%'", _value);
  57.                 }
  58.                 else
  59.                 {
  60.                     if (this.DataType == DataType.Boolen || this.DataType == DataType.Number) return _value;
  61.                     else return string.Format("'{0}'", _value);
  62.                 }
  63.             }
  64.             set { _value = value; }
  65.         }
  66.     }
  67. }

       5、实现接口IFilterString类FilterString.cs

  1. using System;
  2. using System.Text;
  3. using System.Collections;
  4. namespace SearchFilter
  5. {
  6.     /// <summary>
  7.     /// FilterString 的摘要说明
  8.     /// 功能: 实现IFilterString所定义的属性,索引,方法
  9.     /// 作者: xlFancy
  10.     /// 日期: 2008.05.10
  11.     /// 版本: 1.0.0.0
  12.     /// </summary>
  13.     public class FilterString:IFilterString
  14.     {
  15.         private int _count=0;
  16.         private IFilter[] _filter;
  17.         private Hashtable _result = new Hashtable();
  18.         
  19.         /// <summary>
  20.         /// 初始化查询条件生成器
  21.         /// </summary>
  22.         /// <param name="FieldCount">提供选择的字段数量</param>
  23.         public FilterString(int FieldCount)
  24.         {
  25.             _count=FieldCount;
  26.             _filter=new Filter[FieldCount];
  27.         }
  28.         public int FieldCount { get { return _count; } }
  29.         public IFilter this[int Index] 
  30.         { 
  31.             set { _filter[Index]=value; }
  32.             get { return _filter[Index]; }
  33.         }
  34.         public IFilter this[string Key] 
  35.         { 
  36.             set { _result[Key]=value; }
  37.             get { return (IFilter)_result[Key]; } 
  38.         }
  39.         public void FilterClear()
  40.         {
  41.             _result.Clear();
  42.         }
  43.         /// <summary>
  44.         /// 返回查询条件表达式,能自动删除错误的或不匹配的左括弧、右括弧
  45.         /// </summary>
  46.         public string FilterText
  47.         {
  48.             get
  49.             {
  50.                 if (_result.Count==0) return "";
  51.                 string key;
  52.                 StringBuilder sb=new StringBuilder(500);
  53.                 for (int i=0;i<_result.Count;i++)
  54.                 {
  55.                     key=i.ToString();
  56.                     if (this[key].BracketCode.IndexOf("(")!=-1) sb.AppendFormat(" {0}",this[key].BracketCode.TrimStart(')'));
  57.                     sb.AppendFormat(" {0} {1} {2}"this[key].Name, this[key].CompareCode, this[key].Value);
  58.                     if (this[key].BracketCode.IndexOf(")")!=-1) sb.AppendFormat(" {0}",this[key].BracketCode.TrimEnd('('));
  59.                     sb.AppendFormat(" {0}",this[key].LogicalCode);
  60.                 }
  61.                 return BracketMatch(sb.ToString());
  62.             }
  63.         }
  64.         /// <summary>
  65.         /// 清除不匹配的左括弧,右括弧
  66.         /// </summary>
  67.         /// <param name="exp">查询条件表达式</param>
  68.         /// <returns></returns>
  69.         private string BracketMatch(string exp)
  70.         {
  71.             Stack left=new Stack();   //定义空栈,保存左括弧位置
  72.             Stack right=new Stack();   //定义空栈,保存右括弧位置
  73.             for (int i=0;i<exp.Length;i++)
  74.             {
  75.                 switch (exp.Substring(i,1))
  76.                 {
  77.                     case "(":
  78.                         left.Push(i);
  79.                         break;
  80.                     case ")":
  81.                         if (left.Count ==0) right.Push(i);   //栈为空,则当前的右括弧是非法的,将其位置进栈
  82.                         else left.Pop();   //左括弧位置退栈
  83.                         break;
  84.                 }
  85.             }
  86.             //清除不匹配的左括弧
  87.             for (int i=0;i<left.Count;i++) exp=exp.Remove((int)left.Pop(),1);
  88.             //清除不匹配的右括弧
  89.             for (int i=0;i<right.Count;i++) exp=exp.Remove((int)right.Pop(),1);
  90.             return exp.Trim();
  91.         }
  92.     }
  93. }

       6、用户选择查询条件窗体界面

  1. using System;
  2. using System.Data;
  3. using System.Drawing;
  4. using System.Collections;
  5. using System.ComponentModel;
  6. using System.Windows.Forms;
  7. namespace SearchFilter
  8. {
  9.     /// <summary>
  10.     /// SearchFilter 的摘要说明
  11.     /// 功能: 生成查询条件表达式,能自动删除错误的或不匹配的左括弧、右括弧
  12.     /// 作者: xlFancy
  13.     /// 日期: 2008.05.10
  14.     /// 版本: 1.0.0.0
  15.     /// </summary>
  16.     public class SearchFilter : System.Windows.Forms.Form
  17.     {
  18.         private System.ComponentModel.IContainer components;
  19.         private System.Windows.Forms.ComboBox comCompareCode;
  20.         private System.Windows.Forms.TextBox txtValue;
  21.         private System.Windows.Forms.ImageList imageList1;
  22.         private System.Windows.Forms.ToolBar toolBar1;
  23.         private System.Windows.Forms.ToolBarButton tbarAddNew;
  24.         private System.Windows.Forms.ToolBarButton tbarDelete;
  25.         private System.Windows.Forms.ToolBarButton tbarExit;
  26.         private IFilterString _fs;
  27.         private System.Windows.Forms.ComboBox comFieldText;
  28.         private System.Windows.Forms.ComboBox comLogicalCode;
  29.         private System.Windows.Forms.ToolBarButton tbarSep1;
  30.         private System.Windows.Forms.ToolBarButton tbarSearch;
  31.         private System.Windows.Forms.ToolBarButton tbarSep2;
  32.         private System.Windows.Forms.TextBox txtBracketCode;
  33.         private System.Windows.Forms.Button btnBracketLeft;
  34.         private System.Windows.Forms.Button btnBracketRight;
  35.         private System.Windows.Forms.ListView listView1;
  36.         private System.Windows.Forms.ColumnHeader colFieldName;
  37.         private System.Windows.Forms.ColumnHeader colCompareCode;
  38.         private System.Windows.Forms.ColumnHeader colValue;
  39.         private System.Windows.Forms.ColumnHeader colLogicalCode;
  40.         private System.Windows.Forms.ColumnHeader colBracketCode;
  41.         private System.Windows.Forms.ColumnHeader colFieldIndex;
  42.         private System.Windows.Forms.Button btnBracketClear;
  43.         public SearchFilter(IFilterString FilterString)
  44.         {
  45.             //
  46.             // Windows 窗体设计器支持所必需的
  47.             //
  48.             InitializeComponent();
  49.             //
  50.             // TODO: 在 InitializeComponent 调用后添加任何构造函数代码
  51.             //
  52.             _fs=FilterString;
  53.             //初始化字段列表
  54.             for (int i=0;i<_fs.FieldCount;i++) this.comFieldText.Items.Add(_fs[i].Text);
  55.             //设置默认值
  56.             this.comFieldText.SelectedIndex =0;
  57.             this.comCompareCode.SelectedIndex=0;
  58.             this.comLogicalCode.SelectedIndex =0;
  59.         }
  60.         /// <summary>
  61.         /// 清理所有正在使用的资源。
  62.         /// </summary>
  63.         protected override void Dispose( bool disposing )
  64.         {
  65.             if( disposing )
  66.             {
  67.                 if(components != null)
  68.                 {
  69.                     components.Dispose();
  70.                 }
  71.             }
  72.             base.Dispose( disposing );
  73.         }
  74.         #region Windows 窗体设计器生成的代码
  75.         /// <summary>
  76.         /// 设计器支持所需的方法 - 不要使用代码编辑器修改
  77.         /// 此方法的内容。
  78.         /// </summary>
  79.         private void InitializeComponent()
  80.         {
  81.             this.components = new System.ComponentModel.Container();
  82.             System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(SearchFilter));
  83.             this.comFieldText = new System.Windows.Forms.ComboBox();
  84.             this.comCompareCode = new System.Windows.Forms.ComboBox();
  85.             this.txtValue = new System.Windows.Forms.TextBox();
  86.             this.imageList1 = new System.Windows.Forms.ImageList(this.components);
  87.             this.toolBar1 = new System.Windows.Forms.ToolBar();
  88.             this.tbarAddNew = new System.Windows.Forms.ToolBarButton();
  89.             this.tbarDelete = new System.Windows.Forms.ToolBarButton();
  90.             this.tbarSep1 = new System.Windows.Forms.ToolBarButton();
  91.             this.tbarSearch = new System.Windows.Forms.ToolBarButton();
  92.             this.tbarSep2 = new System.Windows.Forms.ToolBarButton();
  93.             this.tbarExit = new System.Windows.Forms.ToolBarButton();
  94.             this.comLogicalCode = new System.Windows.Forms.ComboBox();
  95.             this.txtBracketCode = new System.Windows.Forms.TextBox();
  96.             this.btnBracketLeft = new System.Windows.Forms.Button();
  97.             this.btnBracketRight = new System.Windows.Forms.Button();
  98.             this.btnBracketClear = new System.Windows.Forms.Button();
  99.             this.listView1 = new System.Windows.Forms.ListView();
  100.             this.colFieldName = new System.Windows.Forms.ColumnHeader();
  101.             this.colCompareCode = new System.Windows.Forms.ColumnHeader();
  102.             this.colValue = new System.Windows.Forms.ColumnHeader();
  103.             this.colLogicalCode = new System.Windows.Forms.ColumnHeader();
  104.             this.colBracketCode = new System.Windows.Forms.ColumnHeader();
  105.             this.colFieldIndex = new System.Windows.Forms.ColumnHeader();
  106.             this.SuspendLayout();
  107.             // 
  108.             // comFieldText
  109.             // 
  110.             this.comFieldText.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
  111.             this.comFieldText.Location = new System.Drawing.Point(8, 312);
  112.             this.comFieldText.Name = "comFieldText";
  113.             this.comFieldText.Size = new System.Drawing.Size(200, 21);
  114.             this.comFieldText.TabIndex = 2;
  115.             // 
  116.             // comCompareCode
  117.             // 
  118.             this.comCompareCode.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
  119.             this.comCompareCode.Items.AddRange(new object[] {
  120.                                                                 "LIKE",
  121.                                                                 "NOT LIKE",
  122.                                                                 "<",
  123.                                                                 "=",
  124.                                                                 ">",
  125.                                                                 "<=",
  126.                                                                 ">=",
  127.                                                                 "<>"});
  128.             this.comCompareCode.Location = new System.Drawing.Point(216, 312);
  129.             this.comCompareCode.Name = "comCompareCode";
  130.             this.comCompareCode.Size = new System.Drawing.Size(88, 21);
  131.             this.comCompareCode.TabIndex = 3;
  132.             // 
  133.             // txtValue
  134.             // 
  135.             this.txtValue.Location = new System.Drawing.Point(312, 312);
  136.             this.txtValue.Name = "txtValue";
  137.             this.txtValue.Size = new System.Drawing.Size(120, 23);
  138.             this.txtValue.TabIndex = 4;
  139.             this.txtValue.Text = "";
  140.             // 
  141.             // imageList1
  142.             // 
  143.             this.imageList1.ImageSize = new System.Drawing.Size(20, 20);
  144.             this.imageList1.ImageStream = ((System.Windows.Forms.ImageListStreamer)(resources.GetObject("imageList1.ImageStream")));
  145.             this.imageList1.TransparentColor = System.Drawing.Color.Transparent;
  146.             // 
  147.             // toolBar1
  148.             // 
  149.             this.toolBar1.Appearance = System.Windows.Forms.ToolBarAppearance.Flat;
  150.             this.toolBar1.AutoSize = false;
  151.             this.toolBar1.Buttons.AddRange(new System.Windows.Forms.ToolBarButton[] {
  152.                                                                                         this.tbarAddNew,
  153.                                                                                         this.tbarDelete,
  154.                                                                                         this.tbarSep1,
  155.                                                                                         this.tbarSearch,
  156.                                                                                         this.tbarSep2,
  157.                                                                                         this.tbarExit});
  158.             this.toolBar1.ButtonSize = new System.Drawing.Size(48, 48);
  159.             this.toolBar1.DropDownArrows = true;
  160.             this.toolBar1.ImageList = this.imageList1;
  161.             this.toolBar1.Location = new System.Drawing.Point(0, 0);
  162.             this.toolBar1.Name = "toolBar1";
  163.             this.toolBar1.ShowToolTips = true;
  164.             this.toolBar1.Size = new System.Drawing.Size(666, 50);
  165.             this.toolBar1.TabIndex = 0;
  166.             this.toolBar1.ButtonClick += new System.Windows.Forms.ToolBarButtonClickEventHandler(this.toolBar1_ButtonClick);
  167.             // 
  168.             // tbarAddNew
  169.             // 
  170.             this.tbarAddNew.ImageIndex = 0;
  171.             this.tbarAddNew.Tag = "Add";
  172.             this.tbarAddNew.Text = "Add";
  173.             // 
  174.             // tbarDelete
  175.             // 
  176.             this.tbarDelete.ImageIndex = 1;
  177.             this.tbarDelete.Tag = "Delete";
  178.             this.tbarDelete.Text = "Delete";
  179.             // 
  180.             // tbarSep1
  181.             // 
  182.             this.tbarSep1.Style = System.Windows.Forms.ToolBarButtonStyle.Separator;
  183.             // 
  184.             // tbarSearch
  185.             // 
  186.             this.tbarSearch.ImageIndex = 3;
  187.             this.tbarSearch.Tag = "Search";
  188.             this.tbarSearch.Text = "Search";
  189.             // 
  190.             // tbarSep2
  191.             // 
  192.             this.tbarSep2.Style = System.Windows.Forms.ToolBarButtonStyle.Separator;
  193.             // 
  194.             // tbarExit
  195.             // 
  196.             this.tbarExit.ImageIndex = 4;
  197.             this.tbarExit.Tag = "Exit";
  198.             this.tbarExit.Text = "Exit";
  199.             // 
  200.             // comLogicalCode
  201.             // 
  202.             this.comLogicalCode.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
  203.             this.comLogicalCode.Items.AddRange(new object[] {
  204.                                                                 "AND",
  205.                                                                 "OR"});
  206.             this.comLogicalCode.Location = new System.Drawing.Point(440, 312);
  207.             this.comLogicalCode.Name = "comLogicalCode";
  208.             this.comLogicalCode.Size = new System.Drawing.Size(88, 21);
  209.             this.comLogicalCode.TabIndex = 5;
  210.             // 
  211.             // txtBracketCode
  212.             // 
  213.             this.txtBracketCode.BackColor = System.Drawing.SystemColors.Window;
  214.             this.txtBracketCode.Enabled = false;
  215.             this.txtBracketCode.ForeColor = System.Drawing.SystemColors.WindowText;
  216.             this.txtBracketCode.Location = new System.Drawing.Point(536, 312);
  217.             this.txtBracketCode.Name = "txtBracketCode";
  218.             this.txtBracketCode.ReadOnly = true;
  219.             this.txtBracketCode.Size = new System.Drawing.Size(80, 23);
  220.             this.txtBracketCode.TabIndex = 6;
  221.             this.txtBracketCode.Text = "";
  222.             // 
  223.             // btnBracketLeft
  224.             // 
  225.             this.btnBracketLeft.Location = new System.Drawing.Point(616, 312);
  226.             this.btnBracketLeft.Name = "btnBracketLeft";
  227.             this.btnBracketLeft.Size = new System.Drawing.Size(16, 23);
  228.             this.btnBracketLeft.TabIndex = 7;
  229.             this.btnBracketLeft.Text = "(";
  230.             this.btnBracketLeft.Click += new System.EventHandler(this.btnBracketLeft_Click);
  231.             // 
  232.             // btnBracketRight
  233.             // 
  234.             this.btnBracketRight.Location = new System.Drawing.Point(632, 312);
  235.             this.btnBracketRight.Name = "btnBracketRight";
  236.             this.btnBracketRight.Size = new System.Drawing.Size(16, 23);
  237.             this.btnBracketRight.TabIndex = 8;
  238.             this.btnBracketRight.Text = ")";
  239.             this.btnBracketRight.Click += new System.EventHandler(this.btnBracketRight_Click);
  240.             // 
  241.             // btnBracketClear
  242.             // 
  243.             this.btnBracketClear.Location = new System.Drawing.Point(648, 312);
  244.             this.btnBracketClear.Name = "btnBracketClear";
  245.             this.btnBracketClear.Size = new System.Drawing.Size(16, 23);
  246.             this.btnBracketClear.TabIndex = 9;
  247.             this.btnBracketClear.Text = "X";
  248.             this.btnBracketClear.Click += new System.EventHandler(this.btnBracketClear_Click);
  249.             // 
  250.             // listView1
  251.             // 
  252.             this.listView1.Columns.AddRange(new System.Windows.Forms.ColumnHeader[] {
  253.                                                                                         this.colFieldName,
  254.                                                                                         this.colCompareCode,
  255.                                                                                         this.colValue,
  256.                                                                                         this.colLogicalCode,
  257.                                                                                         this.colBracketCode,
  258.                                                                                         this.colFieldIndex});
  259.             this.listView1.FullRowSelect = true;
  260.             this.listView1.GridLines = true;
  261.             this.listView1.Location = new System.Drawing.Point(0, 54);
  262.             this.listView1.Name = "listView1";
  263.             this.listView1.Size = new System.Drawing.Size(664, 248);
  264.             this.listView1.TabIndex = 1;
  265.             this.listView1.TabStop = false;
  266.             this.listView1.View = System.Windows.Forms.View.Details;
  267.             // 
  268.             // colFieldName
  269.             // 
  270.             this.colFieldName.Text = "FieldName";
  271.             this.colFieldName.Width = 200;
  272.             // 
  273.             // colCompareCode
  274.             // 
  275.             this.colCompareCode.Text = "CompareCode";
  276.             this.colCompareCode.Width = 90;
  277.             // 
  278.             // colValue
  279.             // 
  280.             this.colValue.Text = "Value";
  281.             this.colValue.Width = 130;
  282.             // 
  283.             // colLogicalCode
  284.             // 
  285.             this.colLogicalCode.Text = "LogicalCode";
  286.             this.colLogicalCode.Width = 90;
  287.             // 
  288.             // colBracketCode
  289.             // 
  290.             this.colBracketCode.Text = "BracketCode";
  291.             this.colBracketCode.Width = 90;
  292.             // 
  293.             // colFieldIndex
  294.             // 
  295.             this.colFieldIndex.Text = "FieldIndex";
  296.             this.colFieldIndex.Width = 0;
  297.             // 
  298.             // SearchFilter
  299.             // 
  300.             this.AutoScaleBaseSize = new System.Drawing.Size(7, 16);
  301.             this.ClientSize = new System.Drawing.Size(666, 342);
  302.             this.Controls.Add(this.btnBracketClear);
  303.             this.Controls.Add(this.btnBracketRight);
  304.             this.Controls.Add(this.btnBracketLeft);
  305.             this.Controls.Add(this.txtBracketCode);
  306.             this.Controls.Add(this.txtValue);
  307.             this.Controls.Add(this.comLogicalCode);
  308.             this.Controls.Add(this.toolBar1);
  309.             this.Controls.Add(this.comCompareCode);
  310.             this.Controls.Add(this.comFieldText);
  311.             this.Controls.Add(this.listView1);
  312.             this.Font = new System.Drawing.Font("宋体", 10F);
  313.             this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
  314.             this.MaximizeBox = false;
  315.             this.MinimizeBox = false;
  316.             this.Name = "SearchFilter";
  317.             this.ShowInTaskbar = false;
  318.             this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
  319.             this.Text = "Search Filter";
  320.             this.Closing += new System.ComponentModel.CancelEventHandler(this.SearchFilter_Closing);
  321.             this.ResumeLayout(false);
  322.         }
  323.         #endregion
  324.         private bool IsBoolen(ref string Value)
  325.         {
  326.             try
  327.             {
  328.                 bool b=Convert.ToBoolean(Value);
  329.                 Value=b.ToString();
  330.                 return true;
  331.             }
  332.             catch { return false; }
  333.         }
  334.         private bool IsDateTime(ref string Value)
  335.         {
  336.             try
  337.             {
  338.                 DateTime d=Convert.ToDateTime(Value);
  339.                 Value=d.ToString("yyyy-MM-dd");
  340.                 return true;
  341.             }
  342.             catch { return false; }
  343.         }
  344.         private bool IsNumber(ref string Value)
  345.         {
  346.             try
  347.             {
  348.                 double n=Convert.ToDouble(Value);
  349.                 Value=n.ToString();
  350.                 return true;
  351.             }
  352.             catch { return false; }
  353.         }
  354.         private bool IsValid(DataType Type, ref string Value)
  355.         {
  356.             bool result=false;
  357.             switch (Type)
  358.             {
  359.                 case DataType.Boolen:
  360.                     result=IsBoolen(ref Value);
  361.                     break;
  362.                 case DataType.DataTime:
  363.                     result=IsDateTime(ref Value);
  364.                     break;
  365.                 case DataType.Number:
  366.                     result=IsNumber(ref Value);
  367.                     break;
  368.                 case DataType.String:
  369.                     result=true;
  370.                     break;
  371.             }
  372.             return result;
  373.         }
  374.         private void toolBar1_ButtonClick(object sender, System.Windows.Forms.ToolBarButtonClickEventArgs e)
  375.         {
  376.             switch (e.Button.Tag.ToString())
  377.             {
  378.                 case "Add":
  379.                     AddNew();
  380.                     break;
  381.                 case "Delete":
  382.                     Delete();
  383.                     break;
  384.                 //case "Filter":
  385.                 //  Filter();
  386.                 //  break;
  387.                 case "Search":
  388.                     Search();
  389.                     break;
  390.                 case "Exit":
  391.                     Exit();
  392.                     break;
  393.             }
  394.         }
  395.         private void AddNew()
  396.         {
  397.             string Value=this.txtValue.Text.Trim().Replace("'","");
  398.             if (IsValid(_fs[this.comFieldText.SelectedIndex].DataType, ref Value)==true)
  399.             {   
  400.                 ListViewItem lvi=new ListViewItem(this.comFieldText.Text);
  401.                 lvi.SubItems.Add(this.comCompareCode.Text);
  402.                 lvi.SubItems.Add(Value);
  403.                 lvi.SubItems.Add(this.comLogicalCode.Text);
  404.                 lvi.SubItems.Add(this.txtBracketCode.Text);
  405.                 lvi.SubItems.Add(this.comFieldText.SelectedIndex.ToString());
  406.                 this.listView1.Items.Add(lvi);
  407.                 this.txtBracketCode.Text ="";
  408.             }
  409.             else
  410.             {
  411.                 MessageBox.Show("The " + _fs[this.comFieldText.SelectedIndex].DataType.ToString() + " field value is invalid.",this.Text,MessageBoxButtons.OK,MessageBoxIcon.Warning);
  412.                 this.txtValue.Focus();
  413.             }
  414.         }
  415.         private void Delete()
  416.         {
  417.             if (this.listView1.Items.Count ==0) return;
  418.             if (this.listView1.SelectedItems.Count ==0) return;
  419.             //listView1.Items.RemoveAt(listView1.SelectedItems[0].Index);
  420.             for (int i=this.listView1.Items.Count-1;i>=0;i--)
  421.             {
  422.                 if (this.listView1.Items[i].Selected) this.listView1.Items[i].Remove();
  423.             }
  424.             this.txtBracketCode.Text ="";
  425.         }
  426.         private void FillFilter()
  427.         {
  428.             int m;
  429.             string key;
  430.             _fs.FilterClear();
  431.             
  432.             for (int i=0;i<this.listView1.Items.Count;i++)
  433.             {
  434.                 key=i.ToString();
  435.                 m=Convert.ToInt32(this.listView1.Items[i].SubItems[5].Text);
  436.                 _fs[key]=new Filter(_fs[m].Name, _fs[m].Text, _fs[m].DataType);
  437.                 _fs[key].CompareCode=this.listView1.Items[i].SubItems[1].Text;
  438.                 _fs[key].Value=this.listView1.Items[i].SubItems[2].Text;
  439.                 if (i!=this.listView1.Items.Count-1) _fs[key].LogicalCode=this.listView1.Items[i].SubItems[3].Text;   //去掉最后一个条件的逻辑运算符
  440.                 _fs[key].BracketCode=this.listView1.Items[i].SubItems[4].Text;
  441.             }
  442.         }
  443. //      private void Filter()
  444. //      {
  445. //          FillFilter();
  446. //
  447. //          MessageBox.Show(_fs.FilterText,"Filter expression",MessageBoxButtons.OK,MessageBoxIcon.Information);
  448. //      }
  449.         private void Search()
  450.         {
  451.             FillFilter();
  452.             this.DialogResult=DialogResult.OK;
  453.             this.Close();
  454.         }
  455.         private void Exit()
  456.         {
  457.             this.DialogResult=DialogResult.Cancel;
  458.             this.Close();
  459.         }
  460.         private void btnBracketLeft_Click(object sender, System.EventArgs e)
  461.         {
  462.             this.txtBracketCode.Text =this.txtBracketCode.Text + "(";
  463.         }
  464.         private void btnBracketRight_Click(object sender, System.EventArgs e)
  465.         {
  466.             this.txtBracketCode.Text =this.txtBracketCode.Text + ")";
  467.         }
  468.         private void btnBracketClear_Click(object sender, System.EventArgs e)
  469.         {
  470.             this.txtBracketCode.Text ="";
  471.         }
  472.         private void SearchFilter_Closing(object sender, System.ComponentModel.CancelEventArgs e)
  473.         {
  474.             if (this.DialogResult!=DialogResult.OK) _fs.FilterClear();
  475.         }
  476.     }
  477. }

二、使用方法

  1.             IFilterString fs=new FilterString(7);
  2.             fs[0]=new Filter("UserID","用户ID",DataType.Number);
  3.             fs[1]=new Filter("UserName","用户名",DataType.String);
  4.             fs[2]=new Filter("Dept","部门",DataType.String);
  5.             fs[3]=new Filter("Title","职位",DataType.String);
  6.             fs[4]=new Filter("Account","局域网帐号",DataType.String);
  7.             fs[5]=new Filter("EDate","入厂日期",DataType.DataTime);
  8.             fs[6]=new Filter("Isexp","是否在职",DataType.Boolen);
  9.             Form frm=new SearchFilter(fs);
  10.             frm.ShowDialog();
  11.             string s=fs.FilterText;
  12.             MessageBox.Show(s);

三、源码下载

        http://download.youkuaiyun.com/source/866239

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值