DataView.RowFilter的使用(包括in,like等SQL中的操作符)
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 should 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 = '12/31/2008 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 = '1199.90'" // if current culture is english dataview.rowfilter = "price = '1199,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 * %