星空网 > 软件开发 > ASP.net

简谈回顾多条件搜索查询。(适用于新手,老鸟飘过)

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

  简谈回顾多条件搜索查询。(适用于新手,老鸟飘过)

  其次,我们弄个名字为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 Student10   {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+i27          };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());    }

 

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

    好了。

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

    

 




原标题:简谈回顾多条件搜索查询。(适用于新手,老鸟飘过)

关键词:

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

浅析2020年亚马逊站外推广要点:https://www.ikjzd.com/articles/109263
浅析2020年亚马逊站外推广要点:https://www.ikjzd.com/articles/109264
跨境电商新手卖家必知:跨境电商词汇小科普!:https://www.ikjzd.com/articles/109265
品牌备案后为什么无法做A+?:https://www.ikjzd.com/articles/109266
跨境电商服装行业2020年选品趋势分析!:https://www.ikjzd.com/articles/109267
跨境电商服装行业2020年选品趋势分析!:https://www.ikjzd.com/articles/109268
武陵山大裂谷周围景点 武陵山大裂谷周围景点图片:https://www.vstour.cn/a/411233.html
南美旅游报价(探索南美洲的旅行费用):https://www.vstour.cn/a/411234.html
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流