频道栏目
首页 > 资讯 > 其他综合 > 正文

简谈回顾多条件搜索查询

16-07-12        来源:[db:作者]  
收藏   我要投稿

首先,创建一个这样的界面。

  其次,我们弄个名字为Student的实体类.并在实体类里面弄个静态方法用于模拟数据。

  

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 
 7 namespace 搜索测试
 8 {
 9    public  class Student
10     {
11         public int ID { get; set; }
12         public string Name { get; set; }
13         public int Age { get; set; }
14         public double Height { get; set; } 
15          
16        //模拟数据
17        public static IEnumerable GetStudentData()
18          {
19 
20              for (int i = 1; i <= 12; i++)
21              {
22                  yield return new Student { 
23                      ID=i,
24                      Name=string.Format("同学{0}号",i), 
25                      Age=i*10,
26                      Height=160+i
27                  };
28              } 
29          }
30     }
31 }

   接下来在窗口加载的时候,我们首先把数据赋值给dataGridView。

//获取模拟数据 var data = Student.GetStudentData().ToList(); dataGridView1.DataSource = data;

 

  ---------------------------------------------接下来,我们写单击查询按钮的单击事件-----------------------------------------------------

   代码如下。。。。。。。。。。。。。。

  

 private void button1_Click(object sender, EventArgs e)
        {
            var data = Student.GetStudentData();//.AsQueryable();
 
            if (!string.IsNullOrEmpty(textBox1.Text))
            {
                data = data.Where(item => item.Name.Contains(textBox1.Text));
        
            }

            if (!string.IsNullOrEmpty(textBox2.Text))
            {
                data = data.Where(item => item.Age == int.Parse(textBox2.Text));
            
            }

            if (!string.IsNullOrEmpty(textBox3.Text))
            {
                data = data.Where(item => item.Height == Double.Parse(textBox3.Text));
              
            }

            dataGridView1.DataSource =  data.ToList(); 
        }

     对于上面这个查询,微软会自动帮我们拼接。这些条件,我们只需判断就可以了。

 

    ---------------------------------------------------------------------------拼接sql语句----------------------------------------------

    而对于拼接sql语句,这里我就只是测试一下,弹出拼接的语句。代码如下:

    

  private void button1_Click(object sender, EventArgs e)
        {
            var data = Student.GetStudentData().ToList();

            List where = new List();
            StringBuilder sb = new StringBuilder(" select * from T_Student ");

            // 存储参数
            List listParameters = new List();

            if (!string.IsNullOrEmpty(textBox1.Text))
            {

                where.Add(string.Format(" Name={0} ", textBox1.Text));
                listParameters.Add(new System.Data.SqlClient.SqlParameter("@Name", SqlDbType.NVarChar, 100) { Value = "%" + textBox1.Text.Trim() + "%" });
            }

            if (!string.IsNullOrEmpty(textBox2.Text))
            {

                where.Add(string.Format(" Age={0} ", textBox2.Text));
                listParameters.Add(new System.Data.SqlClient.SqlParameter("@Age", SqlDbType.NVarChar, 100) { Value = "%" + textBox2.Text.Trim() + "%" });
            }

            if (!string.IsNullOrEmpty(textBox3.Text))
            {

                where.Add(string.Format(" Height={0} ", textBox3.Text));
                listParameters.Add(new System.Data.SqlClient.SqlParameter("@Height", SqlDbType.NVarChar, 100) { Value = "%" + textBox3.Text.Trim() + "%" });
            }

            dataGridView1.DataSource = data.ToList();
            if (where.Count > 0)
            {
                sb.Append(" where ");
                sb.Append(string.Join(" and ", where));
            }
            System.Data.SqlClient.SqlParameter[] pms = listParameters.ToArray();

            MessageBox.Show(sb.ToString());

        }

 

     这样,不管用户多少个条件,只要用户筛选了,都会拼接起来。

    好了。

    本文到此结束。。。老鸟飘过哈。

    

 

相关TAG标签
上一篇:php定时计划任务的实现原理
下一篇:Android——微信支付
相关文章
图文推荐

关于我们 | 联系我们 | 广告服务 | 投资合作 | 版权申明 | 在线帮助 | 网站地图 | 作品发布 | Vip技术培训 | 举报中心

版权所有: 红黑联盟--致力于做实用的IT技术学习网站