Skip to content

Dynamic SQL query

George

Asked Apr 11, 2006, 12:40 PM EDT1 answer
Share
LinkedInX
hi i have the following code in my program, that takes an query input to be searched. The user_query can take up to 2 words which are split and put into a string array. How do I build a dynamic SQL query that writes as many arguments as there are words entered into the query. At the moment if i enter only one word to search e.g. "hello" the SQL query will be: select * from Table where Name like "%Hello%" OR Name like which will fail. any help thanks ---------------- string[] query_string= new string[2]; int i=0; char[] delimiterChars = new char[] { ' ' }; string user_query = TextBox1.Text; foreach (string substr in user_query.Split(delimiterChars)) { try { user_query_values[i]= "\"%"+ substr + "%\""; i++; user_query_values[i]= "%"; } catch { Label1.Text="Query too long"; } } string myQuery= ("select * from Table where Name like " + user_query_values[0] + " OR Name like " + user_query_values[1]);

1 Answer

Know the answer? Post it. Somebody with the same question will find it here.

  • +1
    Asalam o alaikum, Just add a check after the loop if(user_query[1] == "" Then user_query[1] = user_query[0] thts it.
    +1