C # sql operator doesn't seem to work
Hello good afternoon. I have a small question on how to describe sql / C #, I cannot figure out why my code is not working.
if (Combo_List.Text == "What")
{
listView1.Items.Clear();
myds.Clear();
mydaEvents.SelectCommand = myconn.CreateCommand();
mydaEvents.SelectCommand.CommandText = "select * from Eventstbl where What like '@what%'";
mydaEvents.SelectCommand.CommandType = CommandType.Text;
mydaEvents.SelectCommand.Parameters.Add("@what", SqlDbType.NVarChar, 2000, "What").Value = text_Search.Text;
mydaEvents.Fill(myds, "Eventstbl");
foreach (DataRow item in myds.Tables["Eventstbl"].Rows)
{
ListViewItem items = new ListViewItem(item["EventsID"].ToString());
items.SubItems.Add(item["What"].ToString());
listView1.Items.Add(items);
}
}
@ what% won't work, but when I put "%" all items that start with the letter a are shown in my list view1. I don't know how to fix this problem. Help me please. Thank you in advance.
+3
Sephiroth111
source
to share
2 answers
Try the following:
mydaEvents.SelectCommand.CommandText = "select * from Eventstbl where What like @what + '%'";
The text inside the quotes will be treated as ... text - you want your parameter to be interpreted as its string value, so you need to move it.
+5
BrokenGlass
source
to share
I guess the problem is what is being '@what%'
treated as a literal and not being replaced with the value of your parameter.
Try doing @what + '%'
this instead.
+5
Daniel Schaffer
source
to share