日期:2014-05-17  浏览次数:20511 次

linq 使用 SqlParameter
用linq操作数据库,能用SqlParameter传参的形式么(为了防注入)
如下面的应该怎样用SqlParameter写
C# code

DataorderDataContext db = new DataorderDataContext();
var ret = from p in db.user where p.name == TextBox1.Text && p.pwd == TextBox2.Text select p;
if (ret.Count() >= 1)
{
    Response.Redirect("index.aspx");
}



------解决方案--------------------
具体的原因很和简单,你将你上边的LINQ语句在执行查询时,查看它生成的SQL语句
你会发现,所有的变量,在"翻译"成SQL语句时,LINQ都自动使用了参数: @p0 @p1 @p2....

所以SQL注入对于LINQ来说,已经不用担心了