create proc P_GetOperator
(@Condition varchar(1000),@PageSize int
)
as
exec('select '+@PageSize+' * from tbl_Operator '+@Condition+'')
go我在asp.net为上述存储过程传查询条件参数
"where OperatorNO like '"+this.textbox1.Text+"%'"
为什么查询不出结果?结果发现在SQL语句执行存储过程时,要这样才可以exec p_GetOperator 5,'where OperatorNo like ''张%'''
为什么要二个单引号?
(@Condition varchar(1000),@PageSize int
)
as
exec('select '+@PageSize+' * from tbl_Operator '+@Condition+'')
go我在asp.net为上述存储过程传查询条件参数
"where OperatorNO like '"+this.textbox1.Text+"%'"
为什么查询不出结果?结果发现在SQL语句执行存储过程时,要这样才可以exec p_GetOperator 5,'where OperatorNo like ''张%'''
为什么要二个单引号?
你最好把"where OperatorNO like '"+this.textbox1.Text+"%'" 最终的值拿到数据库里查一下,看有没有,看看this.textbox1.Text是不是‘张’看看有没有空格。
'select '+@PageSize+' * from tbl_Operator '+@Condition+'',为什么后面还要+''?
加上后面的张%'') 就成了非法sql语句
解决办法
使用两个' 替换一个'
使用两个' 数据库会认为是表示字符 ' 而不是sql语句拼接中用来断句的符号'所有'都要替换成''
建议楼主好好看看 防sql注入 的相关文章,还有 安全sql语句 的相关文章