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.