Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/327.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# C类sql语句不起作用_C#_Sql Server_Sql Like - Fatal编程技术网

C# C类sql语句不起作用

C# C类sql语句不起作用,c#,sql-server,sql-like,C#,Sql Server,Sql Like,你好,大家好。我对sql/c的like语句有一个小问题,我不明白为什么我的代码不能工作 if (Combo_List.Text == "What") { listView1.Items.Clear(); myds.Clear(); mydaEvents.SelectCommand = myconn.CreateCommand(); mydaEvents.SelectCommand.CommandText = "select * from Eventstbl where W

你好,大家好。我对sql/c的like语句有一个小问题,我不明白为什么我的代码不能工作

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%不起作用,但当我放置“a%”时,以字母a开头的所有项目都会显示在我的listview1中。我不知道如何解决这个问题。请帮帮我。提前谢谢。

试试这个:

mydaEvents.SelectCommand.CommandText = "select * from Eventstbl where What like @what + '%'";

引号内的文本将被视为…文本-您希望将参数解释为其字符串值,因此必须将其移出。

我猜问题在于“@what%”被视为文本,而不是被参数值替换


尝试改为执行@what+'%'。

尝试选择。。。比如@what+'%'我看不出代码本身有什么问题。我看不出有什么问题。我也看不到对问题的解释。我忘了说,当我使用“@what%”输入字符或数字时,我的listview中没有显示任何内容,但当我使用“a%”时,从我的listview中显示以字母a开头的所有数据。谢谢你的帮助和一个小的解释。这是我第一次使用like语句。感谢您帮助我,它现在可以工作了: