现在有用户搜索面板若干项给用户定制搜索,下拉表为可选可不选,我准备在NET中判断Request值非空便加入sqlstr +="
sex=@sex and name=@name.....",因为可能要加入5张表,所以觉得这么弄好象有点,是不是用存储过程呢???第一次做搜索,向高手请教,给个思路也可以。
sex=@sex and name=@name.....",因为可能要加入5张表,所以觉得这么弄好象有点,是不是用存储过程呢???第一次做搜索,向高手请教,给个思路也可以。
http://www.cnblogs.com/insus/articles/1999795.html
不过我做的都是 很小的东西
或者参数化查询string sql = @"delete from employees where firstname=@fname and lastname=@lname";
SqlCommand cmdQry = new SqlCommand(sqlqry,conn);
SqlCommand cmdNon = new SqlCommand(sqlInsert,conn);拼接确实不好。
string sqlstr ="where ";
string sex = Request["sex"];
string h1 = Request["height1"];
string h2 = Request["height2"];
...
sqlstr+= "sex="+sex;
if (h1 != null) {
sqlstr += " and Height>=" + h1;
}
if (h2 != null)
{
sqlstr += " and Height<=" + h2;
}
....
set @sql='
select * from ... where 1=1
'
假设有以下几个参数:
@sex int, @name nvarchar(50)if @sex is not null
begin
set @sql = @sql + ' and sex=' + cast(@sex as varchar(5))
end
-- @name传递进来之前,最好做一个Trim()的处理
if @name is not null and len(@name)>0
begin
-- 单引号起转义的作用
set @sql = @sql + ' and name like N''%' + @name + '%'''
end