使用参数化SQL语句进行模糊查找---page_title LIKE @title

本文介绍了使用参数化SQL语句进行模糊查找的正确方法,并对比了两种错误的做法。正确的SQL语句应该在like子句中将通配符 '%' 放在参数后面。

转自:http://www.189works.com/article-47513-1.html

摘要: 今天想用参数化SQL语句进行模糊查找,一开始的使用方法不正确,摸索了好一会。1、使用参数化SQL语句进行模糊查找的正确方法://定义sql语句 string sql = "SELECT StudentID,StudentNO,StudentName FROM Student ...

今天想用参数化SQL语句进行模糊查找,一开始的使用方法不正确,摸索了好一会。

1、使用参数化SQL语句进行模糊查找的正确方法:

     //定义sql语句

      string sql = "SELECT StudentID,StudentNO,StudentName FROM Student WHERE StudentName like @StudentName";

      //给参数赋值

     command.Parameters.AddWithValue("@StudentName", txtStudentName.Text+"%");

2.错误做法1:

     //定义sql语句

      string sql = "SELECT StudentID,StudentNO,StudentName FROM Student WHERE StudentName like '@StudentName%'";

      //给参数赋值

     command.Parameters.AddWithValue("@StudentName", txtStudentName.Text);

3.错误做法2:

     //定义sql语句

      string sql = "SELECT StudentID,StudentNO,StudentName FROM Student WHERE StudentName like @StudentName%";

      //给参数赋值

     command.Parameters.AddWithValue("@StudentName", txtStudentName.Text);

拣尽寒枝不肯栖,寂寞沙洲冷。

 

SELECT DISTINCT 'video' AS type, smv.video_id AS id, smv.video_title AS title, NULL AS channelId, '' AS channelType, '' AS channelName, '' AS channelCode, '' AS shortLink, '' AS longLink, smv.file_id AS fileId, smv.create_time FROM sys_media_video smv LEFT JOIN sys_file_info sfi ON smv.cut_file_id = sfi.file_id LEFT JOIN sys_label sl ON smv.label_id = sl.id LEFT JOIN sys_tag st ON smv.label_id = st.id <where> smv.del_flag = '0' and smv.push_home_page = '1' <if test="title != null and title != ''">AND smv.video_title like concat('%', #{title}, '%')</if> <if test="searchValue != null and searchValue != ''">and smv.video_title like concat('%', #{searchValue},'%')</if> <if test="channelType != null and channelType != ''">AND 1=2</if> <if test="channelName != null and channelName != ''">AND 1=2</if> <if test="channelCode != null and channelCode != ''">AND 1=2</if> <if test="params.beginTime != null and params.beginTime != ''"><!-- 开始时间检索 --> AND smv.create_time >= #{params.beginTime} </if> <if test="params.endTime != null and params.endTime != ''"><!-- 结束时间检索 --> AND smv.create_time <= #{params.endTime} </if> <if test="labelId != null and labelId != ''">and smv.label_id = #{labelId}</if> <if test="labelIds != null and labelIds.length > 0"> and (smv.label_id in <foreach collection="labelIds" item="labelId" open="(" separator="," close=")"> #{labelId} </foreach> ) </if> <if test="tagId != null and tagId != ''">and FIND_IN_SET( #{tagId} , smv.tag_id )</if> <if test="tagIds != null and tagIds.length > 0"> and <foreach collection="tagIds" item="tagId" open="(" separator="," close=")"> FIND_IN_SET( #{tagId} , smv.tag_id ) or </foreach> </if> </where> 修改
最新发布
04-01
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值