读书人

|M| 从零开始学习存储过程第十贴:写了

发布时间: 2011-12-13 21:22:18 作者: rapoo

|M| 从零开始学习存储过程第十贴:写了存储过程,但CSDN朋友都说组SQL不能防止注入和' 那我这写的第一条存储过程要怎么改 谢谢 急
因为自己查询的时候拼写SQL语句的时候不能够防止用户输入 '出错的问题
(又不想去打如 '转成 ' ')
后来听说用SQL存储过程可以解决这个问题
然后学习了存储过程的写法,写了以下一条查询语句

CREATE Procedure COrder_Search(
@OrderID int,
@MemberID int,
@MyClass nvarchar(8),
@MyDayBegin datetime,
@MyDayEnd datetime,
@MyState int,
@MyPayID int,
@MyPostID int,
@MyName nvarchar(20)
)
AS
Declare @sql nvarchar(1000)
Declare @iWhere nvarchar(1000)
Set @sql= 'SELECT * FROM B2COrder '
Set @iWhere= ' Where 1=1 '
if @OrderID <> -1
Begin
Set @iWhere=@iWhere + 'And OrderID = ' + convert(nvarchar,@OrderID)
End
if @MemberID <> -1
Begin
Set @iWhere=@iWhere + 'And MemberID = ' + convert(nvarchar,@MemberID)
End
if @MyClass <> ' '
Begin
Set @iWhere=@iWhere + 'And MyClass = ' ' ' + @MyClass + ' ' ' '
End
if @MyDayBegin > '1900-01-01 '
Begin
Set @iWhere=@iWhere + ' And DateDiff(d,MyDay, ' ' ' + convert(nvarchar,@MyDayBegin ) + ' ' ') <= 0 '
End
if @MyDayEnd > '1900-01-01 '
Begin
Set @iWhere=@iWhere + ' And DateDiff(d,MyDay, ' ' ' + convert(nvarchar,@MyDayEnd )+ ' ' ') > =0 '
End
if @MyState <> -1
Begin
Set @iWhere=@iWhere + 'And MyState = ' + convert(nvarchar,@MyState )
End
if @MyPayID <> -1
Begin
Set @iWhere=@iWhere + 'And MyPayID = ' + convert(nvarchar,@MyPayID )
End
if @MyPostID <> -1
Begin
Set @iWhere=@iWhere + 'And MyPostID = ' + convert(nvarchar,@MyPostID )
End
if @MyName <> ' '
Begin
Set @iWhere=@iWhere + ' And MyName Like ' '% ' + @MyName + '% ' ' '
End
Set @sql=@sql + @iWhere
Execute sp_executesql @sql
GO

像我以上的查询功能要防止注入和 ' 要怎么办呢
谢谢 急

[解决办法]
if CHARINDEX( 'and ', @MyClass)> 0
Return

[解决办法]
帮顶,同样的问题,还没解决思路
[解决办法]
防SQL注入我也没多少经验
我现在就是用验证控件验证或用存储过程验证用户输入的数据是否合法,对字符串类型的数据过滤掉 ' select delect insert update 等关键字
另外 连接时不要用SA做连接
[解决办法]
字符传进存储过程的时候先做相应的替换吧
------解决方案--------------------


路过
[解决办法]
存储过程执行效率高,而且你在调用存储过程的时候,传递的参数就做了格式匹配啊,只需要对字符型的自己在传进参数的时候过滤掉该过滤的东西就好了。
不过存储过程不利于程序移植,用于不用在于你的选择了。
[解决办法]
关注
[解决办法]
mark

[解决办法]
up

[解决办法]
楼主你在给自己找麻烦...
在给存储过程传递参数前,先把把参数值的 '给替换掉,这样不是简单得多吗?
如:db.AddInParameter( cmd , "@Filter " , SqlDbType.VarChar , String.Format( "TITLE = '{0} ' ", " 'aa ".Replace( " ' ", " ' ' " )));
[解决办法]
你这样还不照样跟执行SQL语句一模一样
[解决办法]
对于 ' 单引号 楼主使用存储过程 参数 传递的话

.net实际上 是帮你 已经将 ' 转换成 双引号了的

是完全可以防止这样的注入的了.

楼主将 调用代码和 存储过程一起贴出来
[解决办法]
但是 对于在 外部 就已经 拼接好的字符串.
这样做还是不行
必须
再这之前使用 类似下面的函数 先过滤

/// <summary>
/// 过滤html
/// </summary>
/// <param name= "text "> </param>
/// <returns> </returns>
public static string FilterSQL(string text)
{
string validSql = " ";
if (text != null)
{
text = text.Replace( "\ " ", "" ");
text = text.Replace( "; ", " ");
//text = text.Replace( " ' ", " ' ' ");
// text = text.Replace( "-- ", " ' '-- ' ' ");
text = text.Replace( "%25 ", " ");
text = text.Replace( "%0a ", " ");
text = text.Replace( "%22 ", " ");
text = text.Replace( "%27 ", " ");
text = text.Replace( "%5c ", " ");
text = text.Replace( "%2f ", " ");
text = text.Replace( "%3c ", " ");
text = text.Replace( "%3e ", " ");
text = text.Replace( "%26 ", " ");

text = text.Replace( " < ", "< ");
text = text.Replace( "> ", "> ");
validSql = text;
}
return validSql;
}


[解决办法]
up,JF~~

读书人网 >asp.net

热点推荐