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注入”的安全隐患。

  • 相关阅读:
    minicap编译示例
    uniapp H5项目中使用腾讯地图sdk
    腾讯地图打车乘客端小车平滑移动-安卓篇
    地图定位打卡功能示例
    腾讯位置服务个性化图层创建及发布
    腾讯位置服务GPS轨迹回放
    使用腾讯地图实现汽车沿轨迹行驶功能
    地图GPS轨迹录制
    腾讯地图实现微信小程序地图定位教程
    基于腾讯地图定位组件实现周边POI远近排序分布图
  • 原文地址:https://www.cnblogs.com/bluedoctor/p/1866009.html
Copyright © 2011-2022 走看看