在数据表结果中使用撇号进行搜索查询以过滤数据

huangapple go评论74阅读模式
英文:

Use Apostrophes in search query in datatable result to filter data

问题

I am getting data in one data collection. Now I want to filter it out based on designername value.

But the problem is that in the designername value, I am getting apostrophes, so because of that, it is breaking and not returning data.

Value I am getting in the designername column is this:

Men's Jewelry

Here, if you see in "Men," there are apostrophes, and because of that, I am not getting results.

Using the following line, I am trying to get filtered data.

DataRow[] dr = dtCollections2.Select("designername = '" + strDesigner + "'");

The error I am getting is this:

{"Message":"Syntax error: Missing operand after 's' operator.","StackTrace":" at System.Data.ExpressionParser.Parse()\r\n at System.Data.DataExpression..ctor(DataTable table, String expression, Type type)\r\n at System.Data.Select..ctor(DataTable table, String filterExpression, String sort, DataViewRowState recordStates)\r\n at System.Data.DataTable.Select(String filterExpression)\r\n at ","ExceptionType":"System.Data.SyntaxErrorException"})
英文:

I am getting data in one data collection. Now I want to filter it out based on designername value.

But problem is that in designername value I am getting apostrophes so because of that it is breaking and not returning data.

Value I am getting in desginername column is this :

Men's Jewelry

Here if you see in Men there is apostrophes and because of that I am not getting result.

Using below line I am trying to get filtered data.

DataRow[] dr = dtCollections2.Select("designername = '" + strDesigner + "'");

Error I am getting is this :

{"Message":"Syntax error: Missing operand after \u0027s\u0027 operator.","StackTrace":"   at System.Data.ExpressionParser.Parse()\r\n   at System.Data.DataExpression..ctor(DataTable table, String expression, Type type)\r\n   at System.Data.Select..ctor(DataTable table, String filterExpression, String sort, DataViewRowState recordStates)\r\n   at System.Data.DataTable.Select(String filterExpression)\r\n at "ExceptionType":"System.Data.SyntaxErrorException"})

答案1

得分: 2

Select 方法的文档 引用了此页面上的查询语法示例,从中我们可以看到单引号字符应该使用两个连续的单引号来转义。

字符串值应该用单引号 ' ' 括起来。如果字符串包含单引号 ',则需要双写该引号。

dataView.RowFilter = "Name = 'John'"        // 字符串值
dataView.RowFilter = "Name = 'John ''A'''"  // 带单引号的字符串 "John 'A'"

dataView.RowFilter = String.Format("Name = '{0}'", "John 'A'".Replace("'", "''"));

所以在你的情况下:

DataRow[] dr = dtCollections2.Select("designername = '" + strDesigner.Replace("'", "''") + "'")

英文:

The documentation for the Select method refers to this page for examples of the query syntax, from which we can see that single-quote characters should be escaped by using two of them in a row.

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

So in your case:

DataRow[] dr = dtCollections2.Select("designername = '" + strDesigner.Replace("'", "''") + "'")

huangapple
  • 本文由 发表于 2023年6月13日 00:36:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/76458654.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定