Wild Card%在SQL查询中出错

时间:2013-03-27 13:03:37

标签: c# sql oracle wildcard

用c#编写的这个Oracle SQL查询给出了以下错误:无效字符

qur = " select * from emp where name LIKE '%" + TextBox1.Text + "%'";

我该如何解决这个问题?

2 个答案:

答案 0 :(得分:4)

问题是您的查询对Sql Injection攻击非常开放。由于您没有使用参数化查询,因此在TextBox1中输入的任何内容都可能会破坏您的查询。

例如,如果我输入:'char in Textbox,您的查询将是select * from emp where name LIKE '%'%',它将抛出错误。除此之外,它是漏洞,您不应该使用此类查询。

您可以将查询更改为:

SqlCommand cmd= new SqlCommand( " select * from emp where name LIKE @myParam");
cmd.Parameters.AddWithValue("@myParam", "%" + TextBox1.Text + "%");

你错过了@

How do parameterized queries help against SQL injection?

C# constructing parameter query SQL - LIKE %

答案 1 :(得分:-2)

你应该按如下方式使用它:

qur = " select * from emp where name LIKE '%'" + TextBox1.Text + "'%'";
相关问题