问题描述
我在一个类中构造一个搜索功能,通过我们的一些ASP页面中使用。这个想法很简单,拿来自用户的搜索词,查询数据库的项目。目前我这个做了错误的方式,这是容易受到SQL注入式攻击(和沉船我的系统。
现在,为了做到这一点,正确的做法是作出正确的Oracle变量,通过投入:搜索关键词串并添加以下行:
String concatenation is BAD. Next thing you know, Bobby Tables wrecks my system.Now, the correct way to do this is to to make a proper oracle variable, by putting :searchterm in the string and adding the following line:
cmd.Parameters.Add(New OracleParameter("SEARCHTERM", searchterm))
问题是,因为我用的是一样的语句,我需要能够有对%的搜索词的两侧,而我似乎无法做到这一点与'%:搜索关键词%',它只是给ORA-01036的错误:非法变量名/编号
The problem is since I am using a like statement, I need to be able to have % on either side of the search word, and I can't seem to do that with '%:searchterm%', it just gives an error of ORA-01036: illegal variable name/number.
我可以参数化,但仍然有我喜欢灵活声明成为它的一部分吗?
Can I parameterize but still have my flexible like statement be a part of it?
推荐答案
而不是在你的VB code做级联,做SQL语句中的串联。然后,你所要做的工作应该是什么。下面是一些SQL说明什么,我说什么:
Instead of doing the concatenation in your VB code, do the concatenation in the SQL statement. Then what you're trying to do should work. Here's some SQL illustrating what I'm talking about:
select ID_ELEMENT, ELEMENT_NAME
from table_of_elements
where upper(ELEMENT_NAME) like ('%' || upper(:searchterm) || '%')
顺便说一句,你可以,如果你打开ELEMENT_NAME的collaction不区分大小写的,然后删除对通话结束了更高效的查询上()。
BTW, you might end up with more efficient queries if you switch the collaction on ELEMENT_NAME to case-insensitive and then remove the calls to upper().
这篇关于构建System.Data.OracleClient的使用一个很好的搜索查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!