【问题标题】:Delphi TClientDataSet Locate problemsDelphi TClientDataSet 定位问题
【发布时间】:2011-11-27 10:10:03
【问题描述】:

我正在使用Delphi7MS VistaDevart's dbExpress 驱动程序(4.70 版)。我删除了TSQLConnectionTSQLTable (tabA)、TDataSetProviderTClientDataSet(cdsA)、DataSourceDBGrid

我通过图形设计工具进行了所有设置。一切正常,当我打开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。

弗朗切斯科

【问题讨论】:

    标签: delphi delphi-7 tclientdataset locate


    【解决方案1】:

    您的代码错误。 :)

    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;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-10-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多