C # SQL Multi-conditional query stitching technique

Source: Internet
Author: User

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

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.