build sql query

H

Howard

How do I do this with parameterized query?
without parameterized query:
string search = "hello world search";

search = search.Replace(" ", " AND ");

SELECT * FROM TABLE1 WHERE TEXT LIKE '%' + @search + '%'


parameterized query:

SELECT * FROM TABLE1 WHERE TEXT LIKE '%' + @search + '%'

cmd.Parameters.Add("@search", SqlDbType.NVarChar).Value = search;

basically I am trying to perform a search where all the words in the
search string are present.

Howard
 
S

S. Justin Gengo

Howard,

You specify parameters in a text query with a question mark.

So your sql would look like:

SqlCommand.CommandText = "SELECT * FROM TABLE1 WHERE TEXT LIKE '%'?'%'";
SqlCommand.CommandType = CommandType.Text;
SqlCommand.Parameters.Add("@search", SqlDbType.NVarChar).Value = search;


--
Sincerely,

S. Justin Gengo, MCP
Web Developer / Programmer

www.aboutfortunate.com

"Out of chaos comes order."
Nietzsche
 
B

Bruce Barker

try an array and a loop:


string search = "hello world search";
cmd.CommandText= "select * from table1";
string sep = " where "
string[] terms = search.Split(' ');
for (int i =0; i < terms.length; ++i)
{
string pname = "@search" + i.ToString;
cmd.CommandText += sep + string.format("text like '%' + {0} +
'%'",pname);
cmd.Parameters.Add(pname, SqlDbType.NVarChar).Value = terms;
sep = " or ";
}


-- bruce (sqlwork.com);
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top