This article reproduced: http://blog.csdn.net/limlimlim/article/details/8638080
#region Multi-Conditional search, use the list collection to stitch the conditions (stitching sql) StringBuilder sql = new StringBuilder ("SELECT * from Phonenum"); list<string> wheres = new list<string> (); if (Cbogroup.selectedindex! = 0) {wheres. ADD ("ptypeid=" + cboGroup.Text.Split (' | ') [0]); } if (TxtSearchName.Text.Trim (). Length > 0) {wheres. ADD ("PName like '%" + txtSearchName.Text.Trim () + "% '"); } if (TxtSearchCellPhone.Text.Trim (). Length > 0) {wheres. ADD ("Pcellphone like '%" + txtSearchCellPhone.Text.Trim () + "% '"); }//Determine if the user has selected the condition if (wheres. Count > 0) {string wh = string. Join ("and", wheres. ToArray ()); Sql. Append ("where" + wh); } #endregion #region Multi-conditional search using SQL statements with parameters StringBuilder sql = new StringBuilder ("SELECT * From Phonenum "); list<string> wheres = new list<string> (); list<sqlparameter> listparameter = new list<sqlparameter> (); if (Cbogroup.selectedindex! = 0) {wheres. Add ("[email protected]"); Listparameter.add (New SqlParameter ("@typeid", CboGroup.Text.Split (' | ') [0])); } if (TxtSearchName.Text.Trim (). Length > 0) {wheres. ADD ("PName like @pname"); PName like '% Joe% '//pname liek '% ' [email protected]+ '% ' listparameter.add (new Sqlparam Eter ("@pname", "%" + txtSearchName.Text.Trim () + "%")); } if (TxtSearchCellPhone.Text.Trim (). Length > 0) {wheres. ADD ("Pcellphone like @cellphone"); Listparameter.add (New SqlParameter ("@cellphone", "%" + txtSearchCellPhone.Text.Trim () + "%")); }//Determine if the user has selected the condition if (wheres. Count >0) {string wh = string. Join ("and", wheres. ToArray ()); Sql. Append ("where" + wh); } sqlhelper.executedatatable (SQL. ToString (), Listparameter.toarray ()); #endregion