如何防SQL注入?

解决方案 »

  1.   

    sql语句参数化。。关键字过滤防止用户输出溢出报错只让用户看到错误页看不到错误详细信息
      

  2.   

    关键字过滤 和采用存储过程执行 sql语句
      

  3.   

    string sql = "SELECT * FROM [Table] WHERE [Id]=@id";
    using (SqlConnection con = new SqlConnection(conStr))
    {
       using (SqlCommand cmd = new SqlCommand("Proc_QueryBook", con))
       {
          cmd.Parameters.Add("@Category",SqlDbType.Int).Value=category;
          con.Open();
          using (SqlDataReader dr = cmd.ExecuteReader())
          {
              while(dr.Read()){ //to do something.... }
          }
       }
    }
      

  4.   

    不好意思,那里打错了,cmd.Parameters.Add("@Category",SqlDbType.Int).Value=category;
    这一句里面的@Category应该改成跟sql语句里面的@id
      

  5.   

    SQL参数化操作
    http://topic.csdn.net/u/20090729/14/26381958-0D6E-4B90-BC90-D275E9621F93.html
      

  6.   


    #region sql 语句安全过滤        /// <summary>
            /// 检测SQL注入,字符串型
            /// </summary>
            /// <param name="s">参数</param>
            public string StrCheck2(object s)
            {
                string str1 = "'| and |(|)|--|exec |insert |select |delete |update | count | chr | mid | master |truncate |char |declare |drop |xp_cmdshell |exec master.dbo.xp_cmdshell|net user";
                string str2 = "'| and |(|)|--|exec |insert |select |delete |update | count | chr | mid | master |truncate |char |declare |drop |xp_cmdshell |exec master.dbo.xp_cmdshell|net user";            string[] str1_1 = str1.Split('|');
                string[] str2_1 = str2.Split('|');            string result = "";
                if (s != null)
                {
                    result = s.ToString();
                    for (int i = 0; i < str1_1.Length; i++)
                    {
                        result = result.Replace(str1_1[i], str2_1[i]);
                    }
                    return result;
                }
                else
                    return "";
            }        #endregion 
      

  7.   

    http://www.cnblogs.com/X-Jonney/archive/2009/07/05/1517309.html
      

  8.   

    sql 用参数url参数和表单用函数过滤掉危险sql命令就可以了。
      

  9.   

    可以考虑iis防火墙,效果比较不错的.