Multi-condition query hashtable in asp.net

Source: Internet
Author: User

Whether it is B/S or C/S, there are many conditions in the query, that is, a multi-condition query, many multi-condition queries on the Internet use a large number of if and else, which is not only complicated code, but also inconvenient to read, especially when there are more conditions (more than 10 conditions are used ), I think. Those if and else are obstacles, and poor use affects the running of the entire program ...... now we will introduce a multi-condition query using HashTable. hashTable contains key-value pairs. Why don't we encapsulate the conditions to be queried using HashTable? Then we can traverse HashTable. Isn't that all right? Some people may say, how do you know that the query conditions of others are entered? It's easy, because one feature of HashTable is that it cannot be put into a null value. If there is a null value, it doesn't need to be put into HashTable. In this way, HashTable puts all the input conditions to be queried! You can use the HashTable key value as the query condition !!! [Csharp] /// <summary> /// hash table based on conditions, construct the AND Condition Clause in an SQL statement // </summary> /// <param name = "conditionHash"> condition hash table </param> /// <returns> AND relationship condition Clause </returns> public static String GetConditionClause (Hashtable queryItems) {int Count = 0; String Where = ""; // generate the Condition Clause foreach (DictionaryEntry item in queryItems) cyclically Based on the hash table {if (Count = 0) where = "Where"; else Where + = "And"; // determines whether to add single quotation marks if (item. value. getType (). toString () = "System. string "| item. value. getType (). toString () = "System. dateTime ") {Where + = item. key. toString () + "Like" + SqlStringConstructor. getQuotedString ("%" + item. value. toString () + "%");} else {Where + = item. key. toString () + "=" + item. value. toString ();} Count ++;} return Where ;}

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.