like查询


Hello,

How can I use special characters % and * in DataView.RowFilter
expression in which I have already LIKE statement?

I am using a DataView to displaying a records from datatable, I allow
users to enter the string which will be pasted into RowFilter in such way

MyDataView.RowFilter= myColumn LIKE '%"+MyTextBox.Text+"%'";

The problem is when user enters string which contains % or * I get an
exception:
System.Data.EvaluateException: Error in Like operator: the string
pattern ' ' is invalid.

DataTable can contain strings with those characters and users must be
able to define filter with them.


-------------------------------------------------------------------------------

According to help you should escape both % and * into square brackets: % ->
[%] and * -> [*].

help:
"Both the * and % can be used interchangeably for wildcards in a LIKE
comparison. If the string in a LIKE clause contains a * or %, those
characters should be escaped in brackets ([]). If a bracket is in the
clause, the bracket characters should be escaped in brackets (for example
[[] or []]). "
原文地址:https://www.cnblogs.com/tuantuan/p/418766.html