请问一下传入一个函数的参数为int型,可不可以直接利用int型参数动态构造sql语句而不用SqlParameter传参数如:
Student GetStudeint(int id)
{
   string strSQL = "select * from tbStudent where id=" + id.ToString();
   .......
}这样写不知道有没有SQL注入漏洞,因为参数是int型,应该不会有注入吧?

解决方案 »

  1.   

    呵呵,我都是手工写sql的,根据数据类型定义参数类型,从不用param,个人习惯
    强烈建议
    string sql = String.Format("select * from aaaa where id={0}", id);
    可读和可维护都好
      

  2.   

    写个封装好的方法
    每次调用就传不同的参数 在用string sql = String.Format("select * from aaaa where id={0}", id); 
    还可以先有替换
      

  3.   

    楼主在接收参数的时候首先要将特殊的字符否过滤掉,推荐使用SqlParameter,这样可以对SQL注入有比较好的预防效果。
      

  4.   

    你的ID是int类型的吧,上面的用法:id.ToString(); 是可以被注入的,建议Student GetStudeint(int id) 

       string strSQL = "select * from tbStudent where id=" + convert.ToInt32(id.ToString());   ....... 
    } 这样会进行一个强制转换,如果有恶意注入,转换就会失败,从而停止程序运行,不会对数据库有破坏