zoukankan      html  css  js  c++  java
  • 简谈回顾多条件搜索查询。(适用于新手,老鸟飘过)

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

      

      其次,我们弄个名字为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<Student> 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<Student>(); dataGridView1.DataSource = data; 

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

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

      

     private void button1_Click(object sender, EventArgs e)
            {
                var data = Student.GetStudentData();//.AsQueryable<Student>();
     
                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<Student>();
    
                List<string> where = new List<string>();
                StringBuilder sb = new StringBuilder(" select * from T_Student ");
    
                // 存储参数
                List<System.Data.SqlClient.SqlParameter> listParameters = new List<System.Data.SqlClient.SqlParameter>();
    
                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());
    
            }

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

        好了。

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

        

  • 相关阅读:
    LaTeX公式编辑器
    早期和东京,京都大学高考试题
    猎犬追狐狸试题
    矩阵方程的计算求解(Matlab实现)
    高考压轴题
    何天成:从高联到IMO金牌,超详细数学竞赛学习方法
    几个精彩的数论问题
    高考试题网站
    泛函分析有什么好的教材?
    ifndef系列
  • 原文地址:https://www.cnblogs.com/wzf-Code/p/5660299.html
Copyright © 2011-2022 走看看