zoukankan      html  css  js  c++  java
  • ADO多条件模糊查询防字符串攻击

    多条件组合查询使用字符串拼接TSQL语句来实现

     1 void Button1_Click(object sender, EventArgs e)
     2     {
     3         string text = "select *from car"; //最终TSQL语句
     4         string endtext = ""; //组合查询末尾部分
     5         int num = 0; //记录当前是第几条查询,为了区别前缀是Where还是and
     6         bool ok = false; //判断是否有填写查询
     7         Hashtable hs = new Hashtable(); //哈希表集合,为了对应Parameters的代位符数据
     8 
     9         if (!String.IsNullOrEmpty(txt_ID.Text))//如果文本框的值不为空
    10         {
    11             endtext += "ids like @id"; //在语句末尾阶段拼接上相应的字符串
    12 
    13             //这里注意!模糊查询的通配符需要在集合中拼接添加,不可以直接拼接在语句字符串中!!!
    14             hs.Add("@id", "%" + txt_ID.Text + "%"); 
    15 
    16             ok = true;
    17             num++;
    18         }
    19         if (!string.IsNullOrEmpty(txt_Code.Text))
    20         {
    21             ok = true;
    22             num++;
    23             if (num > 1) //判断是否是第一次拼接查询条件,如果不是第一次,则前缀需要拼接and
    24             {
    25                 endtext += " and code like @code";
    26             }
    27             else
    28             {
    29                 endtext += "code like @code";
    30             }
    31             hs.Add("@code", "%" + txt_Code.Text + "%");
    32         }
    33 
    34         if (!string.IsNullOrEmpty(txt_Name.Text))
    35         {
    36             ok = true;
    37             num++;
    38             if (num > 1)
    39             {
    40                 endtext += " and name like @name";
    41             }
    42             else
    43             {
    44                 endtext += "name like @name";
    45             }
    46             hs.Add("@name", "%" + txt_Name.Text + "%");
    47         }
    48 
    49         if (ok)
    50         {
    51             text = text + " where " + endtext;//这里判断是否有查询条件,如果有就拼接好where
    52         }
    53 
    54         Label1.Text = text; //+ hs["@name"]; 显示查询语句以便于查看拼接结果
    55         list = new CarData().Select(text, hs); //调用查询方法
    56         Repeater1.DataSource = list; //数据指向
    57         Repeater1.DataBind(); //数据绑定
    58     }

    数据访问类中哈希表集合数据导入Parameters集合的方法:

     1 public List<Car> Select(string text,Hashtable hs)
     2     {
     3         List<Car> list = new List<Car>();
     4         cmd.CommandText = text;
     5         cmd.Parameters.Clear();
     6         foreach (string s in hs.Keys) //遍历Keys
     7         {
     8             cmd.Parameters.Add(s,hs[s].ToString()); //将Keys和Values都添加进集合
     9         }
    10 
    11         conn.Open();
    12         SqlDataReader dr = cmd.ExecuteReader();
    13         if (dr.HasRows)
    14         {
    15             while (dr.Read())
    16             {
    17                 Car c = new Car();
    18                 c.Ids = Convert.ToInt32(dr["ids"]);
    19                 c.Code = dr["code"].ToString();
    20                 c.Name = dr["name"].ToString();
    21                 c.Brand = dr["brand"].ToString();
    22                 c.Time = Convert.ToDateTime(dr["time"]);
    23                 c.Oil = Convert.ToDecimal(dr["oil"]);
    24                 c.Powers = Convert.ToInt32(dr["powers"]);
    25                 c.Exhaust = Convert.ToInt32(dr["exhaust"]);
    26                 c.Price = Convert.ToDecimal(dr["price"]);
    27 
    28                 list.Add(c);
    29             }
    30         }
    31 
    32         conn.Close();
    33         return list;
    34     }

    注意!!!

    在TSQL语句拼接时,通配符需要加在Hashtable集合中,不可以直接拼接在字符串中,虽然不报错,但是无结果!!!

  • 相关阅读:
    HDU 2045 不容易系列之(3)—— LELE的RPG难题 (递推)
    HDU 2050 折线分割平面 (递推)
    HDU 5441 Travel (并查集+数学+计数)
    HDU 4597 Play Game (DP,记忆化搜索,博弈)
    HDU 4599 Dice (概率DP+数学+快速幂)
    HDU 4497 GCD and LCM (数学,质数分解)
    UVa 1312 Cricket Field (枚举+离散化)
    HDU 4499 Cannon (暴力求解)
    HDU 4496 D-City (并查集)
    javascript你不知道的知识点
  • 原文地址:https://www.cnblogs.com/xkkk/p/5595003.html
Copyright © 2011-2022 走看看