A brief review of Multi-condition search queries. (Applicable to new users and old birds) and old birds

Source: Internet
Author: User

A brief review of Multi-condition search queries. (Applicable to new users and old birds) and old birds

First, create an interface like this.

Next, let's get an object class named Student, and get a static method in the object class to simulate data.

  

1 using System; 2 using System. collections. generic; 3 using System. linq; 4 using System. text; 5 using System. threading. tasks; 6 7 namespace search test 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 // simulate data 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 ("Student {0}", I), 25 Age = I * 10, 26 Height = 160 + i27}; 28} 29} 30} 31}

Next, when loading the window, we first assign the data value to the dataGridView.

// Obtain the simulated data var data = Student. GetStudentData (). ToList <Student> (); dataGridView1.DataSource = data;

 

------------------------------------------- Next, let's write the Click Event of the query button -----------------------------------------------------

The Code is as follows ..............

  

 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();         }

For the above query, Microsoft will automatically splice it for us. We only need to judge these conditions.

 

--------------------------------------------------------------------------- Concatenate an SQL statement ----------------------------------------------

For concatenated SQL statements, I will test them here. The concatenated statements will pop up. The Code is as follows:

    

Private void button#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 "); // storage parameter 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 ());}

 

In this way, no matter how many conditions the user has, as long as the user has filtered out, it will be spliced.

Okay.

This article ends here... Old birds float.

    

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.