如何避免ClientDataSet Filter中的"表达式中的类型不匹配"

fmm*_*eus 0 delphi tclientdataset filter

运行此代码时出现erro msg"表达式中的类型不匹配":

CDSIndicados.Filtered := False;
CDSIndicados.Filter   := 'EDICOES_ID like ' + QuotedStr(IntToStr(Integer(cxComboBox1.Properties.Items.Objects[cxComboBox1.ItemIndex])));
CDSIndicados.Filtered := True;
Run Code Online (Sandbox Code Playgroud)

我知道当字段的数据类型出错时,可能会出现此消息.但我无法解决.是吗?

Ken*_*ite 6

我怀疑你的EDICOES_ID字段是一个整数值,在这种情况下你不需要在你的过滤器表达式中引用它,并且LIKE不支持运算符AFAIK.如果它是字符串字段,则需要引号并且LIKE受支持,但您通常也希望表达式中包含通配符.(LIKE仅支持字符(字符串)类型字段.对于数字或日期,您需要使用通常的比较运算符>,<,> =,<=,=BETWEEN.)

帮自己一个忙,并声明一个局部变量,并确保ComboBox在尝试访问它之前实际选择了一个项目Objects.我已经为你要检索ItemIndex的类型转换添加了一个和中间存储Object,这使得如果你需要这样做更容易调试.

这是一种解决方案(无论是整数字段还是需要引用的字符串).

var
  Idx, Value: Integer;
begin
  Idx := ComboBox1.ItemIndex;
  if Idx > -1 then
  begin
    CDSIndicados.Filtered := False;
    Value := Integer(cxComboBox1.Properties.Items.Objects[Idx]);

    // If the field is an integer, you don't need a quoted value,
    // and LIKE isn't supported in the filter.
    CDSIndicados.Filter   := 'EDICOES_ID = ' +  IntToStr(Value);

    // Not relevant here, but LIKE isn't supported for date values
    // either. For those, use something like this
    CDSIndicados.Filter := 'EDICOES_DATE = ' + QuotedStr(DateToStr(Value));

    // or, if the field is string and you want LIKE, you need to
    // quote the value and include a wildcard inside that quoted 
    // string.
    CDSIndicados.Filter := 'EDICOES_ID LIKE ' + QuotedStr(IntToStr(Value) + '%');
    CDSIndicados.Filtered := True;
  end;
end;
Run Code Online (Sandbox Code Playgroud)

  • 查看我的编辑(如果这不能解决您的问题,请提供`EDICOES_ID`列的数据类型). (2认同)