DataView.RowFilter的使用(包括in,like等SQL中的操作符)

 更新时间:2011年07月15日 17:13:53   作者:  
这篇blog转自C# examples,对DataView.RowFilter做了详细介绍,能像SQL中使用in,like等操作符一样进行过滤查询,并附有实例,使用方便。

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 * %

相关文章

  • ASP.NET MVC实现本地化和全球化

    ASP.NET MVC实现本地化和全球化

    这篇文章介绍了ASP.NET MVC实现本地化和全球化的方法,文中通过示例代码介绍的非常详细。对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2022-10-10
  • 基于ASP.NET+easyUI框架实现图片上传功能(表单)

    基于ASP.NET+easyUI框架实现图片上传功能(表单)

    这篇文章主要介绍了基于ASP.NET+easyUI框架实现图片上传功能的相关资料,需要的朋友可以参考下
    2016-06-06
  • ASP.NET Cookie是怎么生成的(推荐)

    ASP.NET Cookie是怎么生成的(推荐)

    这篇文章主要介绍了ASP.NET Cookie是怎么生成的,本文给大家介绍的非常详细,具有一定的参考借鉴价值,需要的朋友可以参考下
    2020-01-01
  • asp.net url传递后地址栏乱码(中文超过两个汉字)

    asp.net url传递后地址栏乱码(中文超过两个汉字)

    asp.net 页面传中文超过两个汉字后面就乱码,编码编好的url是正确的,可传到另一个页面就会出错,在地址栏就已经乱码了,本文介绍详细的解决方法,感兴趣的朋友可以了解下,或许对你学习asp.net有所帮助
    2013-02-02
  • ASP.NET MVC5网站开发修改及删除文章(十)

    ASP.NET MVC5网站开发修改及删除文章(十)

    这篇文章主要为大家详细介绍了ASP.NET MVC5网站开发修改及删除文章,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2015-09-09
  • .Net实现延迟队列

    .Net实现延迟队列

    这篇文章介绍了.Net实现延迟队列的方法,文中通过示例代码介绍的非常详细。对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2022-07-07
  • asp.net实现拒绝频繁的IP访问的方法

    asp.net实现拒绝频繁的IP访问的方法

    这篇文章主要介绍了asp.net实现拒绝频繁的IP访问的方法,涉及asp.net针对访问IP的判断及配置文件的设置技巧,需要的朋友可以参考下
    2016-04-04
  • Asp.Net用OWC操作Excel的实例代码

    Asp.Net用OWC操作Excel的实例代码

    这篇文章介绍了Asp.Net用OWC操作Excel的实例代码,有需要的朋友可以参考一下,希望对你有所帮助
    2013-07-07
  • WPF实现左右移动(晃动)动画效果

    WPF实现左右移动(晃动)动画效果

    这篇文章主要为大家详细介绍了WPF实现左右移动或晃动动画效果,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-12-12
  • 基于ABP架构开发的.Net Core项目部署到IIS问题汇总

    基于ABP架构开发的.Net Core项目部署到IIS问题汇总

    这篇文章介绍了基于ABP架构开发的.Net Core项目部署到IIS问题汇总,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2022-06-06

最新评论