zoukankan      html  css  js  c++  java
  • 在SQLMAP中使用动态SQL

    最近有几个同事和朋友询问如何在SQLMAP中“拼接字符串”,因为有时候条件的数量不固定,条件参数类型也不固定,无法写出 @参数名 这样的SQL语句,也就是大家常说的“动态SQL”问题。PDF.NET数据开发框架在1.0版本就支持这个功能了,而且在SQLMAP说明里面也写了,但就是没有人看

    这里举一个实际的例子说明如何使用动态SQL。

    1,设有下面的一个SQLMAP脚本:

    <Select CommandName="GetRemindsBywhere" CommandType="Text" Method="" Description="查询提醒记录根据条件" ResultClass="DataSet"><![CDATA[select a.guid,a.remindttile,a.remindcontent,a.reminddate,
    case when a.isread = 0 then '未处理' else '已处理' end isread,b.customername,c.modelname,b.guid userid
    from WFT_RemindRecord a  
    left join WFT_Customer b on a.customerid = b.guid 
    left join Tb_Common_ModelInfo c on a.remindtypeid = c.modelid
    where 1=1  and   #%tiaojian%# 
    ]]></Select>
        
    </CommandClass>

    使用“替换参数”,仅需要在参数名外面包一个 #%..%# 即可,不需要指定参数的类型,因为“替换”本身就是针对字符串的替换,例如下面的方式是不正确的:
    where 1=1  and   #%tiaojian:String%#

    只需要这样:
    where 1=1  and   #%tiaojian%#

    2,SQLMAP DAL代码:
    使用代码生成工具,上面的SQLMAP脚本将生成下面的DAL代码:

     /// <summary>
        
    /// 查询提醒记录根据条件
        
    /// </summary>
        
    /// <returns></returns>
        public DataSet GetRemindsBywhere(string tiaojian  ) 
        { 
                
    //获取命令信息
                CommandInfo cmdInfo=Mapper.GetCommandInfo("GetRemindsBywhere");
                
    //执行参数替换
                cmdInfo.SetParameterValue("tiaojian", tiaojian, enumParamType.ReplacedText);
                
    //执行查询
                return CurrentDataBase.ExecuteDataSet(CurrentDataBase.ConnectionString, cmdInfo.CommandType, cmdInfo.CommandText ,null);
            
    //
        }//End Function

    从代码可以看出,SQLMAP脚本在红的参数名“tiaojian” 映射成了方法的参数 String tiaojian,而设置参数的方式变成了下面的方式:
    cmdInfo.SetParameterValue("tiaojian", tiaojian, enumParamType.ReplacedText);

    关键之处就是多了一个重载参数:enumParamType.ReplacedText


    使用“替换参数”,在参数数量和参数类型不固定的情况下可以非常灵活的使用,反之则不推荐,尽量使用明确类型的参数,避免带来“SQL注入”的安全隐患。

  • 相关阅读:
    10. Regular Expression Matching
    9. Palindrome Number (考虑负数的情况)
    8. String to Integer (整数的溢出)
    7. Reverse Integer (整数的溢出)
    LeetCode Minimum Size Subarray Sum
    LeetCode Course Schedule II
    Linux 文件缓存 (一)
    LeetCode Tries Prefix Tree
    Linux : lsof 命令
    LeetCode Binary Tree Right Side View
  • 原文地址:https://www.cnblogs.com/bluedoctor/p/1866009.html
Copyright © 2011-2022 走看看