Use of DataView. RowFilter (including operators in SQL such as in and like)

Source: Internet
Author: User

DataView RowFilter Syntax [C #]
This example describes syntax of DataView. RowFil ter expression. It shows how to correctly build expression string (without „ SQL injection ") using methods to escape values.

Column names
If a column name contains any of these special characters ~ () # // => <+-* % & | ^ '"[], You must enclose the column name within square brackets []. if a column name contains right bracket] or backslash/, escape it with backslash (/] or //).

[C #]

DataView. rowFilter = "id = 10"; // no special character in column name "id" dataView. rowFilter = "$ id = 10"; // no special character in column name "$ id" dataView. rowFilter = "[# id] = 10"; // special character "#" in column name "# id" dataView. rowFilter = "[[id/] = 10"; // special characters in column name "[id]"
Literals
String values are enclosed within single quotes ''. If the string contains single quote ', the quote must be doubled.

[C #]

DataView. rowFilter = "Name = 'john'" // string value dataView. rowFilter = "Name = 'john' 'a '''" // string with single quotes "John 'A'" dataView. rowFilter = String. format ("Name = '{0}'", "John 'A '". replace ("'","''"));
Number values are not enclosed within any characters. The values shoshould be the same as is the result of int. ToString () or float. ToString () method for invariant or English culture.

[C #]

DataView. rowFilter = "Year = 2008" // integer value dataView. rowFilter = "Price = 1199.9" // float value dataView. rowFilter = String. format (CultureInfo. invariantCulture. numberFormat, "Price = {0}", 1199.9f );
Date values are enclosed within sharp characters #. The date format is the same as is the result of DateTime. ToString () method for invariant or English culture.

[C #]

DataView. rowFilter = "Date = #12/31/2008 #" // date value (time is 00:00:00) dataView. rowFilter = "Date = #2008-12-31 #" // also this format is supported dataView. rowFilter = "Date = #12/31/2008 16:44:58 #" // date and time value dataView. rowFilter = String. format (CultureInfo. invariantCulture. dateTimeFormat, "Date = # {0} #", new DateTime (2008, 12, 31, 16, 44, 58 ));
Alternatively you can enclose all values within single quotes ''. it means you can use string values for numbers or date time values. in this case the current culture is used to convert the string to the specific value.

[C #]

DataView. rowFilter = "Date = '2014/1/0 16:44:58 '" // if current culture is English dataView. rowFilter = "Date = '31. 12.2008 16:44:58 '"// if current culture is German dataView. rowFilter = "Price = '2017. 90' "// if current culture is English dataView. rowFilter = "Price = '1970, 90'" // if current culture is German
Comparison operators
Equal, not equal, less, greater operators are used to include only values that suit to a comparison expression. You can use these operators = <><=>=.

Note: String comparison is culture-sensitive, it uses CultureInfo from DataTable. locale property of related table (dataView. table. locale ). if the property is not explicitly set, its default value is DataSet. locale (and its default value is current system culture Thread. curren tThread. curren tCulture ).

[C #]

DataView. rowFilter = "Num = 10" // number is equal to 10 dataView. rowFilter = "Date <#1/1/2008 #" // date is less than 1/1/2008 dataView. rowFilter = "Name <> 'john'" // string is not equal to 'john' dataView. rowFilter = "Name> = 'jo '" // string comparison
Operator IN is used to include only values from the list. You can use the operator for all data types, such as numbers or strings.

[C #]

DataView. rowFilter = "Id IN (1, 2, 3)" // integer values dataView. rowFilter = "Price IN (1.0, 9.9, 11.5)" // float values dataView. rowFilter = "Name IN ('john', 'Jim ', 'Tom')" // string values dataView. rowFilter = "Date IN (#12/31/2008 #, #1/1/2009 #)" // date time values dataView. rowFilter = "Id not in (1, 2, 3)" // values not from the list
Operator LIKE is used to include only values that match a pattern with wildcards. wildcard character is * or %, it can be at the beginning of a pattern '* value', at the end 'value', or at both' * value *'. wildcard in the middle of a patern 'va * lue' is not allowed.

[C #]

DataView. rowFilter = "Name LIKE 'J * '" // values that start with 'J' dataView. rowFilter = "Name LIKE '% jo %'" // values that contain 'jo 'dataView. rowFilter = "Name not like 'J * '" // values that don't start with 'J'
If a pattern in a LIKE clause contains any of these special characters * %

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.