George

George

  • NA
  • 3
  • 0

Dynamic SQL query

Apr 11 2006 4:40 PM
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]);

Answers (1)