简单数据集的相关简单查询的实现.

要求:给一个表做查询.很简单.简单到我不知他的什么要求.我那就只好对DataTable做查询了.数据也不知道有什么东东.本来还想对IList<T>做到查询的,有点麻烦,暂时不做了.

先放一个接口.定义了相关查询要的规范,也可以说是契约吗.

ContractedBlock.gif ExpandedBlockStart.gif Code
 1public interface QueryClass
 2ExpandedBlockStart.gifContractedBlock.gif{
 3ExpandedSubBlockStart.gifContractedSubBlock.gif    /**//// <summary>
 4    /// 数据集的栏位
 5    /// </summary>

 6    List<string> Columns
 7ExpandedSubBlockStart.gifContractedSubBlock.gif    {
 8        get;      
 9    }

10ExpandedSubBlockStart.gifContractedSubBlock.gif    /**//// <summary>
11    /// 查询条件
12    /// </summary>

13    string Where
14ExpandedSubBlockStart.gifContractedSubBlock.gif    {
15        get;
16        set;
17    }

18ExpandedSubBlockStart.gifContractedSubBlock.gif    /**//// <summary>
19    /// 数据集
20    /// </summary>

21    object DataSource
22ExpandedSubBlockStart.gifContractedSubBlock.gif    {
23        get;
24        set;
25    }

26ExpandedSubBlockStart.gifContractedSubBlock.gif    /**//// <summary>
27    /// 查询
28    /// </summary>
29    /// <returns></returns>

30    object Select();
31ExpandedSubBlockStart.gifContractedSubBlock.gif    /**//// <summary>
32    /// And查询
33    /// </summary>
34    /// <param name="column"></param>
35    /// <param name="sing"></param>
36    /// <param name="value"></param>
37    /// <returns></returns>

38    string And(string column, string sing, string value);
39    string Or(string column, string sing, string value);
40ExpandedSubBlockStart.gifContractedSubBlock.gif    /**//// <summary>
41    /// 自动完成 
42    /// </summary>
43    /// <param name="column"></param>
44    /// <returns></returns>

45    List<string> AutoComplete(string column);
46ExpandedSubBlockStart.gifContractedSubBlock.gif    /**//// <summary>
47    /// 查询条件变化后引起的事件
48    /// </summary>

49    event Event_Select event_Select;
50ExpandedSubBlockStart.gifContractedSubBlock.gif    /**//// <summary>
51    /// 自动完成选择多少需要的数据
52    /// </summary>

53ExpandedSubBlockStart.gifContractedSubBlock.gif    int AutoCompleteCount get;set;}
54    List<string> AutoComplete(string column, string value);
55}

56ExpandedBlockStart.gifContractedBlock.gif/**//// <summary>
57/// 事件所需的委托
58/// </summary>
59/// <param name="data"></param>
60/// <returns></returns>

61public delegate object Event_Select(object data);

 下面是相关DataTable的实现.如下

ContractedBlock.gif ExpandedBlockStart.gif Code
  1public class QueryTable : QueryClass
  2ExpandedBlockStart.gifContractedBlock.gif{
  3    public event Event_Select event_Select;
  4    private DataTable table;
  5    public object DataSource
  6ExpandedSubBlockStart.gifContractedSubBlock.gif    {
  7        get
  8ExpandedSubBlockStart.gifContractedSubBlock.gif        {
  9            return table;
 10        }

 11        set
 12ExpandedSubBlockStart.gifContractedSubBlock.gif        {
 13            table = value as DataTable;
 14        }

 15    }

 16    public QueryTable()
 17ExpandedSubBlockStart.gifContractedSubBlock.gif    {  
 18    }

 19    private List<string> columns;
 20    public List<string> Columns
 21ExpandedSubBlockStart.gifContractedSubBlock.gif    {
 22        get
 23ExpandedSubBlockStart.gifContractedSubBlock.gif        {
 24            if (columns == null)
 25ExpandedSubBlockStart.gifContractedSubBlock.gif            {
 26                columns = new List<string>();
 27                foreach (DataColumn c in table.Columns)
 28ExpandedSubBlockStart.gifContractedSubBlock.gif                {
 29                    columns.Add(c.ColumnName);
 30                }

 31            }

 32            return columns;
 33        }

 34    }

 35    private string where = string.Empty;
 36    public string Where
 37ExpandedSubBlockStart.gifContractedSubBlock.gif    {
 38        get
 39ExpandedSubBlockStart.gifContractedSubBlock.gif        {
 40            return where;
 41        }

 42        set
 43ExpandedSubBlockStart.gifContractedSubBlock.gif        {
 44            //当改变Where后就会引发函数Select(在里面会引发事件,使包含这个对象的对象也可以做相应的变化)
 45            where = value;
 46            Select();          
 47        }

 48    }
   
 49    public object Select()
 50ExpandedSubBlockStart.gifContractedSubBlock.gif    {
 51        DataTable newtable = new DataTable();
 52        newtable = table.Clone();
 53        DataRow[] rows = table.Select(where);
 54        foreach(DataRow row in rows)
 55ExpandedSubBlockStart.gifContractedSubBlock.gif        {
 56            newtable.ImportRow(row);
 57        }

 58       // DataSource = newtable;
 59        event_Select(newtable);
 60        return newtable;
 61    }

 62    public string Select(string column, string sing, string value)
 63ExpandedSubBlockStart.gifContractedSubBlock.gif    {
 64        return And(column, sing, value);
 65    }

 66    public string And(string column, string sing, string value)
 67ExpandedSubBlockStart.gifContractedSubBlock.gif    {
 68        if (!string.IsNullOrEmpty(where))
 69ExpandedSubBlockStart.gifContractedSubBlock.gif        {
 70            where += " and ";
 71        }

 72        if (sing == "like")
 73ExpandedSubBlockStart.gifContractedSubBlock.gif        {
 74            value = "%" + value + "%";
 75        }

 76        Where += column + " " + sing + " " + "'" + value + "'" + " ";        
 77        return where;
 78    }

 79    public string Or(string column, string sing, string value)
 80ExpandedSubBlockStart.gifContractedSubBlock.gif    {
 81        if (!string.IsNullOrEmpty(where))
 82ExpandedSubBlockStart.gifContractedSubBlock.gif        {
 83            where += " or ";
 84        }

 85        if (sing == "like")
 86ExpandedSubBlockStart.gifContractedSubBlock.gif        {
 87            value = "%" + value + "%";
 88        }

 89        Where += column + " " + sing + " " + "'" + value + "'" + " ";
 90        return where;
 91    }

 92ExpandedSubBlockStart.gifContractedSubBlock.gif    /**//// <summary>
 93    /// 这个是把当前数据表的当前栏位的数据全部加进来,可能给上层去处理分析那些是用的,数据量少时这方法好(这个方法上层只要调用一次就行)
 94    /// </summary>
 95    /// <param name="column"></param>
 96    /// <returns></returns>

 97    public List<string> AutoComplete(string column)
 98ExpandedSubBlockStart.gifContractedSubBlock.gif    {
 99        List<string> items = new List<string>();
100        if (Columns.Contains(column))
101ExpandedSubBlockStart.gifContractedSubBlock.gif        {
102            foreach (DataRow row in table.Rows)
103ExpandedSubBlockStart.gifContractedSubBlock.gif            {
104                string c = row[column].ToString();
105                if (!items.Contains(c))
106                    items.Add(c);
107            }

108        }

109        return items;
110    }

111    private int autoCompleteCount = 0;
112    public int AutoCompleteCount 
113ExpandedSubBlockStart.gifContractedSubBlock.gif    {
114        get
115ExpandedSubBlockStart.gifContractedSubBlock.gif        {
116            if (autoCompleteCount == 0)
117                return int.MaxValue;
118            return autoCompleteCount;
119        }

120        set
121ExpandedSubBlockStart.gifContractedSubBlock.gif        {
122            autoCompleteCount = value;
123        }

124    }

125ExpandedSubBlockStart.gifContractedSubBlock.gif    /**//// <summary>
126    /// 根据当前栏位与输入进来要匹配的字符串来加入,适合数据量大的.实时的直接分析处理.(这个方法需要上层多次来调用,实时更新) 
127    /// </summary>
128    /// <param name="column"></param>
129    /// <param name="value"></param>
130    /// <returns></returns>

131    public List<string> AutoComplete(string column,string value)
132ExpandedSubBlockStart.gifContractedSubBlock.gif    {
133        List<string> items = new List<string>();        
134        if (Columns.Contains(column))
135ExpandedSubBlockStart.gifContractedSubBlock.gif        {
136            foreach (DataRow row in table.Rows)
137ExpandedSubBlockStart.gifContractedSubBlock.gif            {
138                string c = row[column].ToString();
139                if (!items.Contains(c))
140ExpandedSubBlockStart.gifContractedSubBlock.gif                {
141                    if (c.Contains(value))
142ExpandedSubBlockStart.gifContractedSubBlock.gif                    {
143                        items.Add(c);
144                        if (items.Count >= autoCompleteCount)
145                            return items;
146                    }

147                }

148            }

149        }

150        return items;
151    }

152}

上面的实现后,定义一个类,用于与外部的类和上面的接口来交互.如下

ContractedBlock.gif ExpandedBlockStart.gif Code
  1[Serializable]
  2public class QueryData
  3ExpandedBlockStart.gifContractedBlock.gif{
  4    private QueryClass query;
  5    private readonly string str_default = "--请选择--";
  6    private readonly string str_error = "请输入正确的格式";
  7ExpandedSubBlockStart.gifContractedSubBlock.gif    /**//// <summary>
  8    /// 封装查询接口
  9    /// </summary>

 10    public QueryClass Query
 11ExpandedSubBlockStart.gifContractedSubBlock.gif    {
 12        get
 13ExpandedSubBlockStart.gifContractedSubBlock.gif        {
 14            return query;
 15        }

 16        private set
 17ExpandedSubBlockStart.gifContractedSubBlock.gif        {
 18            query = value;
 19        }

 20    }

 21    static QueryData()
 22ExpandedSubBlockStart.gifContractedSubBlock.gif    {
 23        
 24    }

 25    public QueryData(object data)
 26ExpandedSubBlockStart.gifContractedSubBlock.gif    {
 27        //暂时只支持DataTable,而IList接口因为相关有些功能暂时没完成
 28        //用Reflector相看GridView的相关实现(如对DataSocuce对IList的实现)
 29        //没有成功,不知那位有GridView的源码,能编译运行的.希望可以跟踪查看相关实现
 30        if (data is DataTable)
 31ExpandedSubBlockStart.gifContractedSubBlock.gif        {
 32            query = new QueryTable();
 33        }

 34        dataSource = data;
 35        query.DataSource = data;
 36        query.event_Select += new Event_Select(query_event_Select);
 37    }

 38ExpandedSubBlockStart.gifContractedSubBlock.gif    /**//// <summary>
 39    /// 当query对象的查询条件改变时,会引发这个函数使自己的数据集改变
 40    /// </summary>
 41    /// <param name="data">数据集</param>
 42    /// <returns></returns>

 43    public object query_event_Select(object data)
 44ExpandedSubBlockStart.gifContractedSubBlock.gif    {
 45        dataSource = data;
 46        return dataSource;
 47    }

 48    
 49    private object dataSource;
 50ExpandedSubBlockStart.gifContractedSubBlock.gif    /**//// <summary>
 51    /// 数据集
 52    /// </summary>

 53    public object DataSource
 54ExpandedSubBlockStart.gifContractedSubBlock.gif    {
 55        get
 56ExpandedSubBlockStart.gifContractedSubBlock.gif        {
 57            return dataSource;
 58        }

 59        set
 60ExpandedSubBlockStart.gifContractedSubBlock.gif        {
 61            dataSource = value;
 62        }

 63    }

 64ExpandedSubBlockStart.gifContractedSubBlock.gif    /**//// <summary>
 65    /// 数据集的栏位
 66    /// </summary>
 67    /// <param name="list"></param>

 68    public void Columns(DropDownList list)
 69ExpandedSubBlockStart.gifContractedSubBlock.gif    {
 70        list.Items.Clear();
 71        foreach (string c in query.Columns)
 72ExpandedSubBlockStart.gifContractedSubBlock.gif        {
 73            list.Items.Add(c);
 74        }

 75        list.Items.Insert(0, str_default);        
 76    }

 77ExpandedSubBlockStart.gifContractedSubBlock.gif    /**//// <summary>
 78    /// 支持的查询比较
 79    /// </summary>
 80    /// <param name="list"></param>

 81    public void Sign(DropDownList list)
 82ExpandedSubBlockStart.gifContractedSubBlock.gif    {
 83        list.Items.Clear();
 84        list.DataSource = CompareSign.TableSing;
 85        list.DataValueField = "Value";
 86        list.DataTextField = "Key";
 87        list.DataBind();
 88        list.Items.Insert(0, str_default); 
 89    }

 90    private string _value = string.Empty;
 91ExpandedSubBlockStart.gifContractedSubBlock.gif    /**//// <summary>
 92    /// 现暂留
 93    /// </summary>

 94    public string Value
 95ExpandedSubBlockStart.gifContractedSubBlock.gif    {
 96        get
 97ExpandedSubBlockStart.gifContractedSubBlock.gif        {
 98            return _value;
 99        }

100        set
101ExpandedSubBlockStart.gifContractedSubBlock.gif        {
102            _value = value;
103        }

104    }

105ExpandedSubBlockStart.gifContractedSubBlock.gif    /**//// <summary>
106    /// 实现数据And的查询
107    /// </summary>
108    /// <param name="column"></param>
109    /// <param name="sing"></param>
110    /// <param name="_value"></param>
111    /// <returns></returns>

112    public string And(string column,string sing,string _value)
113ExpandedSubBlockStart.gifContractedSubBlock.gif    {
114        if (column == str_default || sing == str_default || string.IsNullOrEmpty(_value))
115            return str_error;
116        Value = _value;
117        query.And(column, sing, _value);
118        return query.Where;
119    }

120ExpandedSubBlockStart.gifContractedSubBlock.gif    /**//// <summary>
121    /// 实现数据Or的查询
122    /// </summary>
123    /// <param name="column"></param>
124    /// <param name="sing"></param>
125    /// <param name="_value"></param>
126    /// <returns></returns>

127    public string Or(string column, string sing, string _value)
128ExpandedSubBlockStart.gifContractedSubBlock.gif    {
129        if (column == str_default || sing == str_default || string.IsNullOrEmpty(_value))
130            return str_error;
131        Value = _value;
132        query.Or(column, sing, _value);
133        return query.Where;
134    }

135
136ExpandedSubBlockStart.gifContractedSubBlock.gif    /**//// <summary>
137    /// 给自动完成的相关接口
138    /// </summary>
139    /// <param name="column"></param>
140    /// <param name="value"></param>
141    /// <returns></returns>

142    public List<string> AutoComplete(string column, string value)
143ExpandedSubBlockStart.gifContractedSubBlock.gif    {
144        return query.AutoComplete(column, value);
145    }

146}

后台就这么多了,本来我想直接做成一个用户控件,可是那样限制太多了.所以就没用了.用了一个表的数据实现了下,效

果可以接受.

页面没什么了,用QueryData类就好了,因为页面刷新后类就有为空了,用ViewState保存就好.我是如下做的.

ContractedBlock.gif ExpandedBlockStart.gif Code
 1    public QueryData queryData
 2ExpandedBlockStart.gifContractedBlock.gif    {
 3        get
 4ExpandedSubBlockStart.gifContractedSubBlock.gif        {
 5            if (ViewState["queryData"!= null)
 6                return (QueryData)ViewState["queryData"];
 7            else
 8ExpandedSubBlockStart.gifContractedSubBlock.gif            {
 9                queryData = new QueryData(你的数据集);
10                return queryData;
11            }

12        }

13        set
14ExpandedSubBlockStart.gifContractedSubBlock.gif        {
15            ViewState["queryData"= value;
16        }

17    }

 应下面一位朋友的要求,因为我没做什么美化,开始不好意思拿出来.差不多如下.

感想:

如果你永远把你的不足给藏着.那么你永远也进步不了.

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值