delphi - Delphi TClientDataSet查找问题

标签 delphi delphi-7 tclientdataset locate

我正在使用Delphi7MS VistaDevart's dbExpress驱动程序(版本4.70)。我删除了TSQLConnectionTSQLTabletabA),TDataSetProviderTClientDataSetcdsA),DataSourceDBGrid

我通过图形设计工具进行了所有设置。一切正常,打开cdsA时,我可以看到Grid中的所有数据。这是我的代码:

procedure TForm1.Button1Click(Sender: TObject);
var
  fields, values: string;
begin
  cdsA.Close;
  cdsA.Open;
  fields := 'fielda;fieldb';
  values := Edit1.Text+';'+Edit2.Text;
  cdsA.SetKey;
  cdsA.Locate(fields, values, [loCaseInsensitive]);
end;


fieldAfieldB在表中存在,并且也在cdsA.Fields中定义。当我执行此代码时,Locate指令会生成异常EVariantInvalidArgError ... Invalid argument。我想知道怎么了。 TIA。

弗朗切斯科

最佳答案

您的代码是错误的。 :)

procedure TForm1.Button1Click(Sender: TObject);
var
  fields, values: string;
begin
  // Closing the CDS and opening it every time is foolish. Just
  // open it if it's not already open.
  if not cdsA.Active then
    cdsA.Open;

  // List of column names. Since column (field) names are always
  // strings, can just use semicolon-separated values
  fields := 'fielda;fieldb'; 

  // Values for columns. Since these could be any type, you can't
  // simply use semicolon-separated strings. You have to pass an
  // array of Variants. The easiest way is to just create it and
  // populate it, and let reference counting release it when it's
  // out of scope
  values := VarArrayOf([Edit1.Text, Edit2.Text]);  

  // No call to SetKey here. SetKey is used with FindKey, not Locate
  cdsA.Locate(fields, values, [loCaseInsensitive]);
end;

关于delphi - Delphi TClientDataSet查找问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8284812/

相关文章:

delphi - 使用 D7 进行远程分析(不是内存分析,而是计时...)

delphi - 将公式从围绕圆的计算修改为围绕椭圆的计算?

delphi - 跟踪修改的行并从 TClientDataSet 的 Delta 手动更新

android - 使用 Delphi for Android 显示 TMediaPlayer.Duration 和 TMediaPlayer.CurrentTime 的秒数

Delphi-如何在按钮单击时调用 ActionList?

delphi - 将Windows消息发送到另一台计算机

database - 如何在运行时更改 Clientdataset 字段数据类型

delphi - 在禁用的 TMemo 控件中启用滚动条

delphi - 指向(子)方法函数的指针?

delphi - 将数据集与 TClientdatasets 嵌套在两个以上级别?