页面多个检索条件下 开发技巧

项目中,经常会遇见查询页面有很多的查询条件,刚入门者通常不知道如何写这些查询,现在以如下图例子说明:

可以看到页面的检索条件如:门,纲,目,科,属,拉丁名,中文名,分布,生物活性,临床报道,药理作用;

SQL语句设计如下:

string.Format("select plantid, phylum, class, plant_order, family, genus, latinname, chinaname, medicinalpart, famt, functionaindi, references, chinasomename, distribute, cultivation, harvestseason, prepare, shapesandproperties, identify, assay, usageanddosage, bioactivity, pharmacological, clinicalreport, createor_id, create_date from PLANTBASIC t WHERE 1=1 {0}", strSql);

 页面后台的方法如下:

View Code
 1  StringBuilder sb = new StringBuilder();
 2         if (!string.IsNullOrEmpty(this.txtPhyLum.Text.Trim()))
 3         {
 4             sb.Append(" AND PHYLUM like '%"+this.txtPhyLum.Text.Trim()+"%'");
 5         }
 6         if (!string.IsNullOrEmpty(this.txtClass.Text.Trim()))
 7         {
 8             sb.Append(" AND CLASS LIKE '%"+this.txtClass.Text.Trim()+"%'");
 9         }
10         if (!string.IsNullOrEmpty(this.txtOrder.Text.Trim()))
11         {
12             sb.Append(" AND PLANT_ORDER LIKE '%"+this.txtOrder.Text.Trim()+"%'");
13         }
14         if (!string.IsNullOrEmpty(this.txtFamily.Text.Trim()))
15         {
16             sb.Append(" AND FAMILY LIKE '%"+this.txtFamily.Text.Trim()+"%'");
17         }
18         if (!string.IsNullOrEmpty(this.txtGenus.Text.Trim()))
19         {
20             sb.Append(" AND GENUS LIKE '%"+this.txtGenus.Text.Trim()+"%'");
21         }
22         if (!string.IsNullOrEmpty(this.txtLatinName.Text.Trim()))
23         {
24             sb.Append(" AND LATINNAME LIKE '%"+this.txtLatinName.Text.Trim()+"%'");
25         }
26         if (!string.IsNullOrEmpty(this.txtCnName.Text.Trim()))
27         {
28             sb.Append(" AND CHINANAME LIKE '%"+this.txtCnName.Text.Trim()+"%'");
29         }
30         if (!string.IsNullOrEmpty(this.txtDistribute.Text.Trim()))
31         {
32             sb.Append(" AND DISTRIBUTE LIKE '%"+this.txtDistribute.Text.Trim()+"%'");
33         }
34         if (!string.IsNullOrEmpty(this.txtBioactivity.Text.Trim()))
35         {
36             sb.Append(" AND BIOACTIVITY LIKE '%"+this.txtBioactivity.Text.Trim()+"%'");
37         }
38         if (!string.IsNullOrEmpty(this.txtClinicalReport.Text.Trim()))
39         {
40             sb.Append(" AND CLINICALREPORT LIKE '%"+this.txtClinicalReport.Text.Trim()+"%'");
41         }
42         this.hidSql.Value = sb.ToString();

通过这样可以很方便检索出结果、无论是多表检索,还是再复杂的检索。

原文地址:https://www.cnblogs.com/hfliyi/p/2602568.html