我如何逃避'和%..? [英] How I Escape ' And % ..?

查看:89
本文介绍了我如何逃避'和%..?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的查询,我无法从查询中转义特殊字符..我想从gridview中的文本框中搜索我需要结果像%somevalue%我的查询不正确plz帮助.. < br $>




从Product.CategoryId = Category.CategoryId ProductName like'中选择ProductName,Description,Image,CategoryName,IsActive from Product inner join Category [%]+ SearchTextBox.Text +[%]'

this is my query and i m not able to escape special characters from the query .. i like to search from a textbox in gridview i need result as like"%somevalue%" my query is not coming right plz help ..


Select ProductName,Description,Image,CategoryName,IsActive from Product inner join Category on Product.CategoryId=Category.CategoryId where ProductName like'[%]"+SearchTextBox.Text+"[%]'

推荐答案

"Select ProductName
,Description,Image
,CategoryName
,IsActive
 
from Product
inner join Category on Product.CategoryId=Category.CategoryId
where ProductName like'%"+SearchTextBox.Text.Replace("%", "[%]")+"%'"


您必须始终在字符串中写入字符串。格式



you should have to write string always in string.Format

 string query=string.Format("Select ProductName
,Description,Image
,CategoryName
,IsActive
from Product
inner join Category on Product.CategoryId=Category.CategoryId
where Product
Name like '%{0}%'",SearchTextBox.Text.Trim());


这将有效,



声明@searchtext varchar(10)='in '

--declare @searchtext varchar(10)='%'

从节假日中选择*其中名称如CASE当@searchtext ='%'然后''Else' %'+ @ searchtext +'%'结束
This will work,

declare @searchtext varchar(10)='in'
--declare @searchtext varchar(10)='%'
select * from holidaytable where name like CASE When @searchtext='%' Then '' Else '%'+@searchtext+'%' End


这篇关于我如何逃避'和%..?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆