Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql server 如何在SQL Server 2008 R2中搜索特殊字符?_Sql Server_Sql Server 2008 R2 - Fatal编程技术网

Sql server 如何在SQL Server 2008 R2中搜索特殊字符?

Sql server 如何在SQL Server 2008 R2中搜索特殊字符?,sql-server,sql-server-2008-r2,Sql Server,Sql Server 2008 R2,我得到的要求是我必须搜索特殊字符,比如名字中有单引号的名字c'te 例: 对于这一点,我得到了如下解决方案 select * from names where name like '%''%' 上面的答案对于搜索姓名中有单引号的人很有用,但我的要求是如何直接搜索姓名 select * from names where name like 'c'test%' 因为从应用程序的角度来看,他们会搜索姓氏d'tech的标签 请注意,在搜索名称时,我们不应传递任何额外的引号 在这方面的帮助与例子将不胜

我得到的要求是我必须搜索特殊字符,比如名字中有单引号的名字
c'te

例:

对于这一点,我得到了如下解决方案

select * from names where name like '%''%'
上面的答案对于搜索姓名中有单引号的人很有用,但我的要求是如何直接搜索姓名

select * from names where name like 'c'test%'
因为从应用程序的角度来看,他们会搜索姓氏d'tech的标签

请注意,在搜索名称时,我们不应传递任何额外的引号

在这方面的帮助与例子将不胜感激


提前感谢

您只需要将它们加倍(我不知道“不应该传递任何额外的引号”是什么意思,但这就是您的做法):

但是,如果使用正确的参数化语句,则不必担心。如果要动态构建查询,只需担心转义单引号,这是不应该做的,因为这样会丢失强类型,并使自己面临SQL注入。快速示例:

string sql=“从名称中选择*,如@name;”;
使用(SqlCommand cmd=newsqlcommand(sql,连接))
{
cmd.Parameters.Add(新的SqlParameter(“Name”,“c'test”));
...
}
select * from names where name like 'c'test%'
select column1,column2 -- don't use *
from dbo.names -- always use schema prefix
where name like 'c''test%'; -- terminate with semi-colon