Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/328.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#_Mysql_Database - Fatal编程技术网

C# 如何返回任何等于或类似的值

C# 如何返回任何等于或类似的值,c#,mysql,database,C#,Mysql,Database,我想在搜索txtbox中输入一个值,并在bank中输入一个类似或相等的值。例如 输入:G 返回:带G的所有名称 如果这个人在打字,你甚至可以写信给乔瓦尼。只有乔瓦尼会回来 我的代码,但它只返回相同的 [...]"select * from users where name = " + txt_name.Text, conn); 使用野生字符搜索,如 如果你想让所有的名字都有你的文字使用: Select names from table where name like '%[your text]

我想在搜索txtbox中输入一个值,并在bank中输入一个类似或相等的值。例如

输入:G

返回:带G的所有名称

如果这个人在打字,你甚至可以写信给乔瓦尼。只有乔瓦尼会回来

我的代码,但它只返回相同的

[...]"select * from users where name = " + txt_name.Text, conn);

使用野生字符搜索,如

如果你想让所有的名字都有你的文字使用:

Select names from table where name like '%[your text]%'
Select names from table where name like '%[your text]%'
如果希望所有名称都以文本开头,请使用:

Select names from table where name like '[your text]%'
如果希望所有名称以文本结尾,请使用:

Select names from table where name like '%[your text]%'
Select names from table where name like '%[your text]%'
请尝试以下操作:

SELECT * FROM users WHERE name LIKE 'G%'
尝试:

顺便说一下,使用
参数
,因为您的代码容易受到的影响,比如:(我在这里使用Sql,所以只需将其更改为mySql连接、适配器等)


当OP指示它应该匹配以文本开头的任何内容时,它将匹配包含文本的任何名称。请仅将通配符放在末尾。。如果名称像“[您的文本]]”,您确实应该考虑使用参数进行查询。
        using (SqlConnection connection = new SqlConnection(connString))
        {
          string sql = "SELECT * FROM users WHERE name LIKE @NameSearch";
            try
            {
                connection.Open();
                using (SqlDataAdapter da = new SqlDataAdapter(sql, connection))
                {

                    using (SqlCommand cmd = new SqlCommand())
                    {
                        da.SelectCommand.Parameters.AddWithValue("@NameSearch", txt_Name.Text+"%");

                            DataTable dt = new DataTable();
                            da.Fill(dt);                                                                   
                    }
                }

            }
            catch (Exception ex)
            {
              MessageBox.Show(ex.Message);
            }

        }