【问题标题】:DataTable expression Cannot interpret token '!'DataTable 表达式无法解释标记“!”
【发布时间】:2014-03-05 22:08:24
【问题描述】:

我有以下代码:

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

我想选择名称为“eric!”的所有行。
这 ”!”给我以下错误:

无法解释标记“!”。

如何选择具有此类标记的所有行?
(我真的需要表达式中的“!”,因为我从 .sql 文件中提取了用户名)

【问题讨论】:

    标签: c# datatable syntax-error expression token


    【解决方案1】:

    您应该在'' 之间使用您的name。喜欢;

    string name = "'eric!'";
    

    如果没有单引号,您的DataTable.Select 方法会认为! 是一个运算符,并且不允许在DataColumn.Expression property 中作为有效运算符。

    来自文档

    用户定义的值

    用户定义的值可以在要比较的表达式中使用 列值。 字符串值应包含在单个 引号。

    【讨论】:

      【解决方案2】:

      你的价值周围缺少引号(' ')

      string name = "eric!";
      string expression = "UserName = '" + name+'";
      DataRow[] result = myDataTable.Select(expression);
      

      当您编写不带引号并使用! 的过滤器运算符时,它会将! 视为不是运算符,这就是它给出错误的原因。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-05-12
        • 2018-04-18
        • 2023-03-27
        • 2013-01-06
        • 2018-09-11
        • 1970-01-01
        • 1970-01-01
        • 2014-03-29
        相关资源
        最近更新 更多