SqlCommand .Parameters 竟然有防止sql注入攻击的功能吗?

unling 2009-04-27 11:27:42
    public static int  GetUser(string name)
{
try
{
DataTable dtData = new DataTable();
SqlConnection sqlc = Connect();

//这样写法错误 比如传入的参数是 1 ' or '1'='1 将会把表emploreer 中所有数据都删除掉or '1'='1 总会成立
//string strSql = "delete from emploreer where username='"+name+"'";

//我这样写用了Parameters 竟然删除条数为0就是没有被全部删除掉
string strSql = "delete from emploreer where username=@p1";

SqlCommand sqlcom = new SqlCommand(strSql, sqlc);
sqlcom.Parameters.Add("@p1", SqlDbType.VarChar, 50);
sqlcom.Parameters["@p1"].Value = name;

sqlc.Open();
int c = sqlcom.ExecuteNonQuery();
sqlc.Close();
return c;
}
catch (Exception ex)
{

throw;
}

}

SqlCommand .Parameters 竟然有防止sql注入攻击的功能吗?还试巧合
...全文
323 17 打赏 收藏 转发到动态 举报
写回复
用AI写文章
17 条回复
切换为时间正序
请发表友善的回复…
发表回复
unling 2009-04-28
  • 打赏
  • 举报
回复
[Quote=引用 16 楼 starfd 的回复:]
引用 10 楼 xzsnj 的回复:
有所悟,以前还真是没有注意到区别,那不知道用

string.format("delete from emploreer where username='{0}'",name)

会有什么结果,楼主试验一下看看

' or 1=1 or '' = '

把你所有的数据全部删光~~~~ohye
[/Quote]

string.format("delete from emploreer where username='{0}'",name) ;
string strSql = "delete from emploreer where username='"+name+"'";


两个一样都是会全部删掉数据
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 xzsnj 的回复:]
有所悟,以前还真是没有注意到区别,那不知道用

string.format("delete from emploreer where username='{0}'",name)

会有什么结果,楼主试验一下看看
[/Quote]
' or 1=1 or '' = '

把你所有的数据全部删光~~~~ohye
  • 打赏
  • 举报
回复
哇。。。。好巧。。。。。。。
Tensionli 2009-04-28
  • 打赏
  • 举报
回复
使用参数可以防止SQL注入
llsen 2009-04-28
  • 打赏
  • 举报
回复
有防止注入的功能
但是不全部
goldxinx 2009-04-28
  • 打赏
  • 举报
回复
防注入
烈火蜓蜻 2009-04-28
  • 打赏
  • 举报
回复
明白的晚了点,不过,总比不知道强
xzsnj 2009-04-28
  • 打赏
  • 举报
回复
有所悟,以前还真是没有注意到区别,那不知道用

string.format("delete from emploreer where username='{0}'",name)

会有什么结果,楼主试验一下看看
wartim 2009-04-28
  • 打赏
  • 举报
回复
参数把引号当数据
而直接写会把引号当sql语句一部分
怎么是巧合
所以注入无非就是利用....'+ +'......中间插入一段... 或...'....'...等等的么
....'...'....'... '...... 只要引号匹配
FantasyFreed 2009-04-28
  • 打赏
  • 举报
回复
这个可以有~~
claymore1114 2009-04-27
  • 打赏
  • 举报
回复
革命尚未成功,我们 还需努力。加油吧
yangqidong 2009-04-27
  • 打赏
  • 举报
回复
你竟然不知道吗?
gisyellow 2009-04-27
  • 打赏
  • 举报
回复
参数的主要功能之一。。
PandaIT 2009-04-27
  • 打赏
  • 举报
回复
嗯。
JackLeiMM 2009-04-27
  • 打赏
  • 举报
回复
恩,是的有防注入效果的,最起码不能拼SQL语句了
HDNGO 2009-04-27
  • 打赏
  • 举报
回复
哇,好巧好巧~
伤心的小水沟 2009-04-27
  • 打赏
  • 举报
回复




嗯,有,不是巧合

111,125

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Creator Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧