.Net 过滤器绑定源

发布于 2024-10-12 08:17:27 字数 219 浏览 6 评论 0原文

如果我像这样过滤 BindingSource 的列:"column1 LIKE '%John%'",则该列上包含 John 的所有行都将返回。

如何返回 column1 包含 [some text]John[some text]Doe 的所有行?

“column1 LIKE '%John%Doe'” 不起作用。

If I filter a BindingSource's column like this: "column1 LIKE '%John%'", then all rows containing John on that column will return.

How to return all rows for which column1 contains [some text]John[some text]Doe ?

"column1 LIKE '%John%Doe'" does not work.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

我是有多爱你 2024-10-19 08:17:27

根据 http://msdn.microsoft.com 上的文档/en-us/library/system.data.datacolumn.expression.aspx

通配符 * 和 %
可以互换使用
LIKE 中的通配符
比较。如果 LIKE 中的字符串
子句包含 * 或 %,那些
字符应包含在
括号 ([])。如果括号位于
子句,每个括号字符应该
用括号括起来(例如
[[] 或者 []])。允许使用通配符
模式的开始和结束,或者在
模式的末尾或开头
的一个模式。例如:

“商品名称类似于“产品””

“商品名称类似于‘*产品’”

“商品名称如'产品*'”

不允许使用通配符
字符串的中间。例如,
不允许使用“text*xt”。

因此,你不能使用 LIKE 来做你想做的事。您的下一个最佳选择是获取字符串表示形式并使用正则表达式来解析它以查找您想要的文本。那会很慢。

你可以这样做

column1 LIKE '%John%' AND LIKE %Doe%'

,但逻辑不同,可能不是你真正想要的。

编辑 - 添加

您最好在服务器级别进行过滤,因为您的数据库可能支持字符串中间的通配符。我刚刚在我们的 SQL Server 上尝试过,效果很好。 (SQL Server 2005)

Per the documentation at http://msdn.microsoft.com/en-us/library/system.data.datacolumn.expression.aspx

Wildcard Characters Both the * and %
can be used interchangeably for
wildcard characters in a LIKE
comparison. If the string in a LIKE
clause contains a * or %, those
characters should be enclosed in
brackets ([]). If a bracket is in the
clause, each bracket character should
be enclosed in brackets (for example
[[] or []]). A wildcard is allowed at
the start and end of a pattern, or at
the end of a pattern, or at the start
of a pattern. For example:

"ItemName LIKE 'product'"

"ItemName LIKE '*product'"

"ItemName LIKE 'product*'"

Wildcard characters are not allowed in
the middle of a string. For example,
'te*xt' is not allowed.

Therefore, you can't use LIKE to do what you want. Your next best bet is to get the string representation and use a Regex to parse it looking for the text you want. That's going to be SLOW.

You COULD do

column1 LIKE '%John%' AND LIKE %Doe%'

but the logic is different and may not be what you really want.

Edit - added

You might be better off doing the filtering at the server level, as your DB might support a wildcard in the middle of the string. I just tried it on our SQL Server and it works fine. (SQL Server 2005)

数理化全能战士 2024-10-19 08:17:27

试试这个

   "ItemName LIKE '*john*' and  ItemName LIKE '*doe*'"

try this

   "ItemName LIKE '*john*' and  ItemName LIKE '*doe*'"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文