Note that there are some explanatory texts on larger screens.

plurals
  1. POSQL search with logical operators
    primarykey
    data
    text
    <p>I have a asp.net web application that needs a search page that searches for data in a specific table (SQL). Today it only has a simple LIKE query, useing parametized sql:</p> <pre><code>SqlParameter[] param = new SqlParameter[1]; param[0] = new SqlParameter("@searchText", SqlDbType.VarChar); param[0].Value = "%" + text + "%"; using (SqlDataReader dr = SqlHelper.ExecuteReader(this.ConnectionString, CommandType.StoredProcedure, "isp_Search", param)) { //Do something } </code></pre> <p>Now I need to add the option to use logical operators to the search.</p> <p>So in the textbox a user might search for things like </p> <pre><code>Adam OR Adams James AND NOT Jame Douglas AND (Adam OR Adams) </code></pre> <p>Adding full-text indexing to the table is not a preferred option since I do not control the data model.</p> <p>I'm looking for a method of interpreting the text queries and convert into a SQL statement with the appropriate number of SqlParams.</p> <pre><code>searchexpression = "Douglas AND (Adam OR Adams)" MakeSearchQuery(searchexpression, out sqlquery, out SqlParam[] params) </code></pre> <p>Would return somelike like</p> <pre><code>sqlquery = "SELECT someFields FROM table WHERE someField=@Param1 AND (someField=@Param2 OR someField=@Param3)" </code></pre> <p>And a sqlParams like</p> <pre><code>sqlParam[0] = 'Douglas' sqlParam[1] = 'Adam' sqlParam[2] = 'Adams' </code></pre> <p>Now there must be someone that have made something like this before? I've search both SO and Google without any real success. And pointers to either open source/free code or a good idea on how to convert the search expression into SQL is welcome.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload