原文出自: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'"

  1. 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

  1. 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

  1. dataView.RowFilter

= String.Format(CultureInfo.InvariantCulture.DateTimeFormat, "Date = #{0}#", newDateTime(, , , , , ));

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

(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

  1. 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

  1. 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'

  1. 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 = ; i < valueWithoutWildcards.Length; i++) { char c = valueWithoutWildcards[i]; if (c == '*' || c == '%' || c == '[' || c == ']') sb.Append("[").Append(c).Append("]"); else if (c == '\'') sb.Append("''"); else

  1. sb.Append(c);
  2. }
  3. return sb.ToString();
  4. }

(c#)

  1. //

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开始在指定点

20160122 DataView RowFilter语法的更多相关文章

  1. DataView RowFilter Syntax [C#]

    RowFilter语法 private void btnEnquiry_Click(object sender, EventArgs e) { string filterExpression = st ...

  2. DataView.RowFilter筛选DataTable中的数据

    //定义一个DataView ,得到一个全部职员的视图DataView dataView1 = DbHelperSQL.QueryDataView(sql); //过滤得到一个只显示男职员的视图 da ...

  3. DataView RowFilter

    DataView类用来表示定制的DataTable的视图. DataTable和DataView的关系是遵循著名的设计模式--文档/视图模式,其中DataTable是文档,而Dataview是视图. ...

  4. .NET中的视图和过滤器 (DefaultView和RowFilter)

    NET中的视图和过滤器 (DefaultView和RowFilter) ADO.NET中有一层对象,用来创建任意数据源的抽象模型.其中包括DataSet,DataTable,DataRow,DataV ...

  5. [转载]DataView详解

    表示用于排序.筛选.搜索.编辑和导航的 DataTable 的可绑定数据的自定义视图. DataView的功能类似于数据库的视图,他是数据源DataTable的封装对象,可以对数据源进行排序.搜索.过 ...

  6. .net窗体程序的基础知识及详细笔记

    第一章:初识Windows程序 1.1:第一个wondows程序 1.1.1:认识windows程序 Form1.cs:窗体文件:程序对窗体编写的代码一般都存放在这个文件(还有拖动控件时的操作和布局, ...

  7. C# 对Datatable排序

    一,在C#中要对Datatable排序,可使用DefaultView的Sort方法.先获取Datatable的DefaultView,然后设置 得到的Dataview的sort属性,最后用视图的ToT ...

  8. 浅谈DevExpress<二>:设计一个完整界面(2)

    下面来把剩下的工作做完,换肤功能昨天已近讨论过,今天就不重复了.首先建立三个全局变量,一个存放文件路径,一个存放数据,一个存放过滤条件. string DBFileName; DataView dat ...

  9. Winform带dataGridview的Combox控件

    调用控件: public partial class Form1 : Form { public Form1() { InitializeComponent(); //---------------- ...

随机推荐

  1. Redis集合的常用操作指令

    Redis集合的常用操作指令 Sets常用操作指令 SADD 将指定的元素添加到集合.如果集合中存在该元素,则忽略. 如果集合不存在,会先创建一个集合然后在添加元素. 127.0.0.1:6379&g ...

  2. 当系统开启safe_mode和 open_basedir

    当系统开启safe_mode和 open_basedir,在程序中使用以下语句curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);并且遇到301,302状态吗时 ...

  3. VS中发布并调试IIS程序

    1.创建本地IIS站点 2.修改配置 .net framework 右击项目属性,服务器修改为本地IIS,并且项目URL修改为相对应的站点即可 .net core 右键属性,进入调试栏新建一个配置 选 ...

  4. Linux常用指令全集

    Linux简介及Ubuntu安装 常见指令 系统管理命令 打包压缩相关命令 关机/重启机器 Linux管道 Linux软件包管理 vim使用 用户及用户组管理 文件权限管理 大牛笔记-www.weix ...

  5. js中数组遍历常用的方法

    常见的数组遍历方法,比如 for in,for  of, forEach,map,filter,every,some,find,reduce等 1,普通for循环,经常用的数组遍历 var arr = ...

  6. kali优化配置(3)--工具箱

    1.netcat 收集信息.Telnet/banner.传输文本信息.连接服务器端口. *通过IP,连接服务器端口: *信息通信: *重定向符号:> (e.g:>>ps.txt:重定 ...

  7. Sql 统计一个表有多少列

    SELECT COUNT(syscolumns.name) FROM syscolumns , sysobjects WHERE syscolumns.id = sysobjects.id AND s ...

  8. C# Windows Services 启动和结束其它进程

    将exe所在的绝对路径和进程名配置到配置文件中 <add key="FilePath" value="D:\ABC\ABCD.Console.exe"/& ...

  9. Could not resolve all files for configuration ':app:debugCompileClasspath'.解决方案

    异常如下: Error:FAILURE: Build failed with an exception. * What went wrong:Could not resolve all files f ...

  10. 【LeetCode】按 tag 分类索引 (900题以下)

    链表:https://www.cnblogs.com/zhangwanying/p/9797184.html (共34题) 数组:https://www.cnblogs.com/zhangwanyin ...