防止sql注入的方法有哪些?? 常用的有哪些?给小段代码哈 O(∩_∩)O谢谢 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 public static bool SafeCode(string code) { string ostr = "', ,=,%,declare,set,--"; string[] str = Regex.Split(ostr, ","); bool _re = true; for (int i = 0; i < str.Length; i++) { if (code.ToLower().IndexOf(str[i]) > -1) { _re = false; return false; } } return _re; } public bool validate(string str) { string strobj; bool test = false; for (int i = 0; i < str.Length; i++) { strobj = str.Substring(i, 1); if (strobj == "%" || strobj == "&" || strobj == "'" || strobj == "|" || strobj == "<" || strobj == ">") { test = true; break; } } return test; } @canshu 参数化过滤字符也行! 一般sql.replace("'", "''");就可以了. sql参数化http://topic.csdn.net/u/20090729/14/26381958-0D6E-4B90-BC90-D275E9621F93.html asp里面我用函数过滤 .net刚接触 学习 用存储过程就不会出现SQL注入啦或者用下面这个方法也可以防止 一般的SQL注入 public static bool CheckSqlClauseIsValid(string strSqlClause) { int j; string s1, s2; string[] arrInvalid = { "insert ", "delete ", "update ", "exec ", "execute ", "drop ", "grant ", "backup ", "restore ", " sp_", " xp_" }; strSqlClause = strSqlClause.ToLower().TrimStart(); for (int i = 0; i < arrInvalid.Length; i++) { j = 0; while ((j = strSqlClause.IndexOf(arrInvalid[i], j + 1)) > -1) { s1 = strSqlClause.Substring(0, j); s2 = s1.Replace("'", ""); if ((s1.Length - s2.Length) % 2 == 0) return false; } } return true; } 对于高级的嘛,你要自己去研究SQL 注入的 一些原理 不要拼接sql 吧存储过程等 参数化查询,linq等都可以防注入。 1。字符串查找,替换2。存储过程都可以实现SQL注入可以使用一些扫描工具扫一下,比如啊D 等 IsMatch()正则表达式问题,高手进! 插件无法显示 JQuery获得table行的数据 在客户端用脚本实现hyperlink的navigateurl 动态变化? 高难度问题 牛人来 请问vs2005,asp.net怎么让textbox的内容显示为* 属性里怎么找不到呢? 动态生成HTML文件的问题 急 用个函数计算下面格式的公式。 !!!如何将Word文件在IE中打开 !!! 怎么更改DataGrid的列的宽度?不让字段内容长的自动换行!!! 问两个关于remoting的问题 如何在当前结果中进行查找
public static bool SafeCode(string code)
{
string ostr = "', ,=,%,declare,set,--";
string[] str = Regex.Split(ostr, ","); bool _re = true; for (int i = 0; i < str.Length; i++)
{
if (code.ToLower().IndexOf(str[i]) > -1)
{
_re = false;
return false;
}
}
return _re;
}
public bool validate(string str)
{
string strobj;
bool test = false;
for (int i = 0; i < str.Length; i++)
{
strobj = str.Substring(i, 1);
if (strobj == "%" || strobj == "&" || strobj == "'" || strobj == "|" || strobj == "<" || strobj == ">")
{
test = true;
break;
}
}
return test;
}
过滤字符也行!
sql.replace("'", "''");
就可以了.
http://topic.csdn.net/u/20090729/14/26381958-0D6E-4B90-BC90-D275E9621F93.html
{
int j;
string s1, s2;
string[] arrInvalid = { "insert ", "delete ", "update ", "exec ", "execute ", "drop ", "grant ", "backup ", "restore ", " sp_", " xp_" };
strSqlClause = strSqlClause.ToLower().TrimStart();
for (int i = 0; i < arrInvalid.Length; i++)
{
j = 0;
while ((j = strSqlClause.IndexOf(arrInvalid[i], j + 1)) > -1)
{
s1 = strSqlClause.Substring(0, j);
s2 = s1.Replace("'", "");
if ((s1.Length - s2.Length) % 2 == 0) return false; }
}
return true;
} 对于高级的嘛,你要自己去研究SQL 注入的 一些原理
存储过程等
2。存储过程
都可以实现SQL注入可以使用一些扫描工具扫一下,比如啊D 等