本文介绍了如果我在字符串变量中有值,如%'s1'%,那么如何使用like运算符是对的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

SqlDataAdapter adpcn = new SqlDataAdapter("select PAGE_DESC from OccCONTRACT where SERVICES like '% s1 %'", ConfigurationManager.ConnectionStrings["cn"].ConnectionString);
        DataTable dtcn = new DataTable();
        adpcn.Fill(dtcn);





我尝试过:



从OccCONTRACT选择PAGE_DESC,其中像'%s1%'这样的服务



What I have tried:

select PAGE_DESC from OccCONTRACT where SERVICES like '% s1 %'

推荐答案

'% s1 %'



但是如果你想匹配This1是匹配的行那么你将使用


However if you want to match "This1 is a matching line" then you would use

'%s1%'

因为您不关心s1之前(或之后)是否有空格



如果您只想将开始的行与s1(s1匹配此行)匹配,则删除第一个%

because you don't care if there is a space before (or after) the "s1"

If you want to only match rows that start with "s1" ("s1 match this line") then remove the first %

's1%'

或者,如果您只想要以s1结尾的行,则不要使用最后一个%ie

OR if you only want lines that end with "s1" then don't use the last % i.e.

'%s1'





您已经说过要在字符串变量中匹配的值...如果 s1 实际上是变量的名称(而不是您正在尝试的文本)匹配)然后你想要这样的东西(关于空格的注释和上面%字符的数量仍然适用):



You've said that you have the value to match in a string variable ... if s1 is actually the name of your variable (and not the text you are strying to match) then you want something like this (the comments about spaces and the number of % characters above still apply):

string query =
    String.Format("select PAGE_DESC from OccCONTRACT where SERVICES like '%{0}%'",s1);
SqlDataAdapter adpcn =
    new SqlDataAdapter(query, 
    ConfigurationManager.ConnectionStrings["cn"].ConnectionString);


这篇关于如果我在字符串变量中有值,如%'s1'%,那么如何使用like运算符是对的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-27 13:23