20160122 DataView RowFilter语法

原文出自:http://www.csharp-examples.net/dataview-rowfilter/

DataView RowFilter语法(c#)

这个例子描述了DataView.RowFil­ter表达式的语法。它显示了如何正确构建表达式字符串(不„SQL注入”)转义字符的使用方法。

列名

如果一个列名包含这些特殊字符 ~ ( ) # \ / = > < + - * % & | ^ ' " [ ],你必须在列名上加上方括号[ ]。如果一个列名包含右括号 ]或反斜杠 \,用反斜杠转义(\] \\).

(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]"

文字

字符型(String):使用单引号包裹’’。如果字符串包含单引号’,使用两个单引号。

(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("'", "''"));

数值型:不需要使用特殊符号包裹。的值应该是一样的结果 int.ToString() float.ToString()方法不变或英语文化。

(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);

日期型:使用井号字符包裹 # #。日期格式是一样的结果 DateTime.ToString()方法不变或英语文化。

(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}#", newDateTime(2008, 12, 31, 16, 44, 58));

或者你可以使用单引号包裹' '。这意味着您可以使用数字或日期时间值的字符串值。在这种情况下,当前英文用于将字符串。

(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

比较运算符

平等,不等,小于,大于的运营商用于只包括适合比较表达式的值。您可以使用这些操作符 = <> < <= > >=.

注意:字符串比较是与文化相关的,它使用的CultureInfoDataTable.Locale房地产相关的表(dataView.Table.Locale)。如果没有显式地设置属性,它的默认值是数据集。地区(及其默认值是当前系统文化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

运算符IN:是用来从列表中只包含值。您可以使用操作员对所有数据类型,如数字或字符串。

(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

运算符LIKE:是用来只包含值与通配符匹配一个模式。通配符是 * %,它可以在一个模式的开始 '*value',最后'value*',或者在两侧 '*value*'。通配符在中间 是不允许的'va*lue'。

(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'

如果包含这些特殊字符 * % [ ],这些字符必须在括号中 [ ]像这样 [*], [%], [[][]].

(c#)

dataView.RowFilter = "Name LIKE '[*]*'"// values that starts with '*' dataView.RowFilter = "Name LIKE '[[]*'"// values that starts with '['

下面的方法转义一个文本的使用的例子。

(c#)

public static stringEscapeLikeValue(string valueWithoutWildcards) { StringBuilder sb = newStringBuilder(); for (int i = 0; i < valueWithoutWildcards.Length; i++) { char c = valueWithoutWildcards[i]; if (c == '*' || c == '%' || c == '[' || c == ']') sb.Append("[").Append(c).Append("]"); else if (c == '\'') sb.Append("''"); else

      sb.Append(c);
  }
  return sb.ToString();
}

(c#)

//
select all that starts with the value string (in this case with "*")string value = "*"; // the dataView.RowFilter will be: "Name LIKE '[*]*'" dataView.RowFilter = String.Format("Name LIKE '{0}*'"EscapeLikeValue(value));

布尔操作符

布尔操作符 AND, OR NOT用于连接表达式。运算符 NOT它优先于运算符AND优先于运算符OR。

(c#)

// operator AND has precedence over OR operator, parenthesis are needed dataView.RowFilter = "City = 'Tokyo' AND (Age < 20 OR Age > 60)"; // following examples do the same dataView.RowFilter = "City <> 'Tokyo' AND City <> 'Paris'"; dataView.RowFilter = "NOT City = 'Tokyo' AND NOT City = 'Paris'"; dataView.RowFilter = "NOT (City = 'Tokyo' OR City = 'Paris')"; dataView.RowFilter = "City NOT IN ('Tokyo', 'Paris')";

算术和字符串操作符

算术运算符是加法 +、减 -、乘 *、除 /和模量 %.

(c#)

dataView.RowFilter = "MotherAge - Age < 20"; // people with young mother dataView.RowFilter = "Age % 10 = 0"; // people with decennial birthday

还有一个字符串运算符连接 +.

亲子关系引用

但事情表可以使用父表达式中引用的列名称 Parent.前缀。Acolumn子表中可以使用孩子列名称引用 Child.前缀。

对孩子的引用列必须在一个聚合函数,因为孩子的关系可能返回多个行。例如表达 SUM(Child.Price)返回的所有价格在parenttable行相关的子表。

如果一个表有一个以上的孩子关系,前缀名称必须包含关系。例如表达Child(OrdersToItemsRelation).Price引用列价格在子表中使用关系叫OrdersToItemsRe­副调制。

聚合函数

有支持的聚合函数 SUM, COUNT, MIN, MAX, AVG(平均) STDEV(统计标准偏差) VAR(统计方差)。

这个例子展示了singletable聚合函数执行。

(c#)

// select people with above-average salary dataView.RowFilter = "Salary > AVG(Salary)";

下面的例子显示了聚合函数对两个表进行亲子关系。假设有表订单和项目与亲子关系。

(c#)

// select orders which have more than 5 items dataView.RowFilter = "COUNT(Child.IdOrder) > 5"; // select orders which total price (sum of items prices) is greater or equal $500 dataView.RowFilter = "SUM(Child.Price) >= 500";

功能

也有支持以下功能。详细描述可以在这里找到DataColumn.Ex­压力.

  • CONVERT——将特定的表达式来指定的。净Frameworktype
  • LEN——得到一个字符串的长度
  • ISNULL——检查表达式并返回检查表达式或replacementvalue
  • IIF——得到两个值之一取决于逻辑表达式的结果
  • TRIM——删除所有前导和尾随空白字符\ r、\ n \ t,‚”
  • SUBSTRING——指定长度的子串,在thestring开始在指定点

 

posted @ 2016-01-22 15:23  Tirisfal  阅读(795)  评论(0编辑  收藏  举报