DataTable表达式无法解释令牌'!' [英] DataTable expression Cannot interpret token '!'

查看:83
本文介绍了DataTable表达式无法解释令牌'!'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码:

//myDataTable has the following collumns: UserName, Birthday and email.
string name = "eric!";
string expression = "UserName = " + name;
DataRow[] result = myDataTable.Select(expression);

我想选择所有名称为 eric!的行。

!给我以下错误:

I want to select all rows with the name "eric!".
The "!" gives me the following error:


无法解释令牌!。

Cannot interpret token "!".

如何选择带有此类标记的所有行?

(由于从.sql文件中提取了用户名,因此我确实需要在表达式中加上!)

How can I select all rows with such tokens?
(I really need the "!" in the expression since I extract the userNames from a .sql file)

推荐答案

您应在之间使用您的名称''。像;

You should use your name between ''. Like;

string name = "'eric!'";

没有单引号,您的 DataTable.Select 方法认为运算符,并且 DataColumn.Expression 属性作为有效运算符。

Without single quotes, your DataTable.Select method thinks that ! is an operator and it is not allowed in DataColumn.Expression property as a valid operator.

来自文档


用户定义的值

User-Defined Values

可以在表达式中使用用户定义的值与
列值进行比较。 字符串值应该用单引号
引起来。

User-defined values may be used within expressions to be compared with column values. String values should be enclosed within single quotation marks.

这篇关于DataTable表达式无法解释令牌'!'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆