Delphi TClientDataSet定位问题

发布于 2024-12-18 08:45:01 字数 874 浏览 3 评论 0原文

我正在使用 Delphi7MS VistaDevart's dbExpress 驱动程序(版本 4.70)。我删除了一个 TSQLConnection、一个 TSQLTable (tabA)、一个 TDataSetProvider、一个 TClientDataSet(cdsA)、一个DataSource 和一个DBGrid

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

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。

弗朗西斯科

I'm using Delphi7, MS Vista and Devart's dbExpress drivers (version 4.70). I drop a TSQLConnection, a TSQLTable (tabA), a TDataSetProvider, a TClientDataSet(cdsA), a DataSource and a DBGrid.

I made all the settings via graphical design tool. Everything works fine, as I open cdsA, I can see all the data in the Grid. Here is my code:

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;

fieldA and fieldB exists in table and are defined in cdsA.Fields too. When I execute this code the Locate instruction generate exception EVariantInvalidArgError ... Invalid argument. I'm wondering what's wrong. TIA.

Francesco

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

合久必婚 2024-12-25 08:45:01

你的代码是错误的。 :)

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;

Your code is wrong. :)

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;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文