C# 如何返回任何等于或类似的值
我想在搜索txtbox中输入一个值,并在bank中输入一个类似或相等的值。例如 输入:G 返回:带G的所有名称 如果这个人在打字,你甚至可以写信给乔瓦尼。只有乔瓦尼会回来 我的代码,但它只返回相同的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]
[...]"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);
}
}