Click here to Skip to main content
15,913,115 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Im using stringbuilder to build my where clause.

How to implement a like clause using stringbuilder?.

For example - I want this query executed.
Select * from table where agentName like '@agentName%';

I would like to implement - agentName like '@agentName%' implement which is part of my whereclause.

C#
Strinbuilder sb = new StringBuilder();
sb.Append(agentName LIKE @agentName);
cmd.Parameters.AddWithValue("@agentName",agentName;);


Can you please help?.

Arvind.
Posted

C#
sb.Append("agentName LIKE @agentName");

then
C#
cmd.Parameters.AddWithValue("@agentName", agentName +"%");
 
Share this answer
 
Comments
Member 10562713 5-Jul-14 5:19am    
Thanks! This did the trick :) !!! Appreciated!
Try:
C#
StringBuilder sb = new StringBuilder();
sb.Append("SELECT * FROM myTable WHERE ");
...
sb.Append("agentName LIKE @agentName + '%' ");
...
using (SqlCommand cmd = new SqlCommand(sb.ToString(), con))
   {
   cmd.Parameters.AddWithValue("@agentName", agentName);
   ...
 
Share this answer
 
Comments
Member 10562713 5-Jul-14 4:12am    
This didnt work. Im not getting any result from db. Its always Null but I know data is there.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900