zoukankan      html  css  js  c++  java
  • 关于GridView防止用户SQL注入的方法

    //下面是我写的一个关于GridView的例子;
    int rid = Convert.ToInt32(this.GridView1.DataKeys[e.RowIndex].Value.ToString());
    string rtitle = ((TextBox)(this.GridView1.Rows[e.RowIndex].Cells[1].Controls[0])).Text;
    string rauthor = ((TextBox)(this.GridView1.Rows[e.RowIndex].Cells[2].Controls[0])).Text;
    string rtime = ((TextBox)(this.GridView1.Rows[e.RowIndex].Cells[3].Controls[0])).Text;
     
    //我写的一条sql执行语句,里面的rid 、rtitle 、rauthor 、rtime 是从GridView控件里面获取的,
    string sqlCon = "update news set ntitle='" + rtitle + "',nauthor='" + rauthor + "',ntime='" + rtime + "' where nid=" + rid;
     
    //如果用户在rtitle输入  '--'  的话就变成
    [code=SQL]update news set ntitle=''--'',nauthor='--',ntime='2007/8/15 0:00:00' where nid=31
    //这样的话数据库就彻底崩溃了
     
    //现在我把他改成调用存储过程的方式来修改,
    int rid = Convert.ToInt32(this.GridView1.DataKeys[e.RowIndex].Value.ToString());
    string rtitle = ((TextBox)(this.GridView1.Rows[e.RowIndex].Cells[1].Controls[0])).Text;
    string rauthor = ((TextBox)(this.GridView1.Rows[e.RowIndex].Cells[2].Controls[0])).Text;
    string rtime = ((TextBox)(this.GridView1.Rows[e.RowIndex].Cells[3].Controls[0])).Text;
    using (SqlConnection con = new SqlConnection(conStr))
    {
          string procName = "proc_updatenews";
          SqlCommand com = new SqlCommand(procName, con);
          com.CommandType = CommandType.StoredProcedure;
          com.Parameters.AddWithValue("@rid", rid);
          com.Parameters.AddWithValue("@rtitle", rtitle);
          com.Parameters.AddWithValue("@rauthor", rauthor);
          com.Parameters.AddWithValue("@rtime", rtime);

           con.Open();
           result = com.ExecuteNonQuery();
    }
    //这样执行的结果却是好的。。
  • 相关阅读:
    1101-Trees on the Level
    1099-移动小球
    1096-组合数
    Windows环境配置Apache+Mysql+PHP
    ArtDialog简单使用示例
    实现数字与字母的随机数
    SQLServer2005:在执行批处理时出现错误。错误消息为: 目录名无效
    sql语句总结
    在SQL SErver中实现数组功能
    aspnet_regiis.exe 的用法
  • 原文地址:https://www.cnblogs.com/wanzhongjun/p/6406240.html
Copyright © 2011-2022 走看看