1、看一下ClientDataset在快驴客户端的容器作用:    下面这个函数根据实体类的属性定义ClientDataset内的表结构,相当于建了一个容器,等待您盛数据://
// Create client dataset for the table...
class function TInvoices.CreateCds: TClientDataset;
begin
  Result:=TClientDataset.Create(nil);
  with Result do
     begin
        with FieldDefs.AddFieldDef do
           begin
              Name:='ShipName';
              DataType:=ftWideString;
              Size:=40;
           end;
        with FieldDefs.AddFieldDef do
           begin
              Name:='ShipAddress';
              DataType:=ftWideString;
              Size:=60;
           end;
        with FieldDefs.AddFieldDef do
           begin
              Name:='ShipCity';
              DataType:=ftWideString;
              Size:=15;
           end;
        with FieldDefs.AddFieldDef do
           begin
              Name:='ShipRegion';
              DataType:=ftWideString;
              Size:=15;
           end;
        with FieldDefs.AddFieldDef do
           begin
              Name:='ShipPostalCode';
              DataType:=ftWideString;
              Size:=10;
           end;
        with FieldDefs.AddFieldDef do
           begin
              Name:='ShipCountry';
              DataType:=ftWideString;
              Size:=15;
           end;
        with FieldDefs.AddFieldDef do
           begin
              Name:='CustomerID';
              DataType:=ftWideString;
              Size:=5;
           end;
        with FieldDefs.AddFieldDef do
           begin
              Name:='CustomerName';
              DataType:=ftWideString;
              Size:=40;
           end;
        with FieldDefs.AddFieldDef do
           begin
              Name:='Address';
              DataType:=ftWideString;
              Size:=60;
           end;
        with FieldDefs.AddFieldDef do
           begin
              Name:='City';
              DataType:=ftWideString;
              Size:=15;
           end;
        with FieldDefs.AddFieldDef do
           begin
              Name:='Region';
              DataType:=ftWideString;
              Size:=15;
           end;
        with FieldDefs.AddFieldDef do
           begin
              Name:='PostalCode';
              DataType:=ftWideString;
              Size:=10;
           end;
        with FieldDefs.AddFieldDef do
           begin
              Name:='Country';
              DataType:=ftWideString;
              Size:=15;
           end;
        with FieldDefs.AddFieldDef do
           begin
              Name:='Salesperson';
              DataType:=ftWideString;
              Size:=31;
           end;
        with FieldDefs.AddFieldDef do
           begin
              Name:='OrderID';
              DataType:=ftInteger;
           end;
        with FieldDefs.AddFieldDef do
           begin
              Name:='OrderDate';
              DataType:=ftDateTime;
           end;
        with FieldDefs.AddFieldDef do
           begin
              Name:='RequiredDate';
              DataType:=ftDateTime;
           end;
        with FieldDefs.AddFieldDef do
           begin
              Name:='ShippedDate';
              DataType:=ftDateTime;
           end;
        with FieldDefs.AddFieldDef do
           begin
              Name:='ShipperName';
              DataType:=ftWideString;
              Size:=40;
           end;
        with FieldDefs.AddFieldDef do
           begin
              Name:='ProductID';
              DataType:=ftInteger;
           end;
        with FieldDefs.AddFieldDef do
           begin
              Name:='ProductName';
              DataType:=ftWideString;
              Size:=40;
           end;
        with FieldDefs.AddFieldDef do
           begin
              Name:='UnitPrice';
              DataType:=ftBCD;
              Size:=4;
           end;
        with FieldDefs.AddFieldDef do
           begin
              Name:='Quantity';
              DataType:=ftSmallint;
           end;
        with FieldDefs.AddFieldDef do
           begin
              Name:='Discount';
              DataType:=ftFloat;
           end;
        with FieldDefs.AddFieldDef do
           begin
              Name:='ExtendedPrice';
              DataType:=ftBCD;
              Size:=4;
           end;
        with FieldDefs.AddFieldDef do
           begin
              Name:='Freight';
              DataType:=ftBCD;
              Size:=4;
           end;
        CreateDataSet;
     end;
  Result.Open;
end;
2、根据Cds中的当前记录生成实体类对象实例:    根据ClientDataset的当前记录内容,生成一个实体类的对象示例,返回给调用程序://
// Current Record to an object...
class function TInvoices.ReadFromCds(aCds: TClientDataset): TInvoices;
begin
  if (not aCds.active) or (aCds.RecordCount<=0) then
    begin
      Result:=nil;
      exit;
    end;
  Result := TInvoices.Create;
  with Result do
    begin
      f_ShipName := Trim(aCds.FieldByName('ShipName').AsString);
      f_ShipAddress := Trim(aCds.FieldByName('ShipAddress').AsString);
      f_ShipCity := Trim(aCds.FieldByName('ShipCity').AsString);
      f_ShipRegion := Trim(aCds.FieldByName('ShipRegion').AsString);
      f_ShipPostalCode := Trim(aCds.FieldByName('ShipPostalCode').AsString);
      f_ShipCountry := Trim(aCds.FieldByName('ShipCountry').AsString);
      f_CustomerID := Trim(aCds.FieldByName('CustomerID').AsString);
      f_CustomerName := Trim(aCds.FieldByName('CustomerName').AsString);
      f_Address := Trim(aCds.FieldByName('Address').AsString);
      f_City := Trim(aCds.FieldByName('City').AsString);
      f_Region := Trim(aCds.FieldByName('Region').AsString);
      f_PostalCode := Trim(aCds.FieldByName('PostalCode').AsString);
      f_Country := Trim(aCds.FieldByName('Country').AsString);
      f_Salesperson := Trim(aCds.FieldByName('Salesperson').AsString);
      f_OrderID := aCds.FieldByName('OrderID').AsInteger;
      f_OrderDate := aCds.FieldByName('OrderDate').AsDateTime;
      f_RequiredDate := aCds.FieldByName('RequiredDate').AsDateTime;
      f_ShippedDate := aCds.FieldByName('ShippedDate').AsDateTime;
      f_ShipperName := Trim(aCds.FieldByName('ShipperName').AsString);
      f_ProductID := aCds.FieldByName('ProductID').AsInteger;
      f_ProductName := Trim(aCds.FieldByName('ProductName').AsString);
      f_UnitPrice := aCds.FieldByName('UnitPrice').AsFloat;
      f_Quantity := aCds.FieldByName('Quantity').AsInteger;
      f_Discount := aCds.FieldByName('Discount').AsFloat;
      f_ExtendedPrice := aCds.FieldByName('ExtendedPrice').AsFloat;
      f_Freight := aCds.FieldByName('Freight').AsFloat;
    end;
end;
3、将实体类对象写入Cds容器    将一个实体类对象写入ClientDataset,生成一个数据库记录。本函数假定对应的数据表结构已经在ClientDataset中定义完毕,ClientDataset并处于打开状态://
// Object to Current Record...
function TInvoices.WriteToCds(Cds: TClientDataset): Boolean;
begin
   try
     Cds.edit;
     Cds.FieldValues['ShipName']:=ff_ShipName;
     Cds.FieldValues['ShipAddress']:=ff_ShipAddress;
     Cds.FieldValues['ShipCity']:=ff_ShipCity;
     Cds.FieldValues['ShipRegion']:=ff_ShipRegion;
     Cds.FieldValues['ShipPostalCode']:=ff_ShipPostalCode;
     Cds.FieldValues['ShipCountry']:=ff_ShipCountry;
     Cds.FieldValues['CustomerID']:=ff_CustomerID;
     Cds.FieldValues['CustomerName']:=ff_CustomerName;
     Cds.FieldValues['Address']:=ff_Address;
     Cds.FieldValues['City']:=ff_City;
     Cds.FieldValues['Region']:=ff_Region;
     Cds.FieldValues['PostalCode']:=ff_PostalCode;
     Cds.FieldValues['Country']:=ff_Country;
     Cds.FieldValues['Salesperson']:=ff_Salesperson;
     Cds.FieldValues['OrderID']:=ff_OrderID;
     Cds.FieldValues['OrderDate']:=ff_OrderDate;
     Cds.FieldValues['RequiredDate']:=ff_RequiredDate;
     Cds.FieldValues['ShippedDate']:=ff_ShippedDate;
     Cds.FieldValues['ShipperName']:=ff_ShipperName;
     Cds.FieldValues['ProductID']:=ff_ProductID;
     Cds.FieldValues['ProductName']:=ff_ProductName;
     Cds.FieldValues['UnitPrice']:=ff_UnitPrice;
     Cds.FieldValues['Quantity']:=ff_Quantity;
     Cds.FieldValues['Discount']:=ff_Discount;
     Cds.FieldValues['ExtendedPrice']:=ff_ExtendedPrice;
     Cds.FieldValues['Freight']:=ff_Freight;
     Cds.post;
     result:=true;
   except
     result:=false;
   end;
end;

解决方案 »

  1.   


    4、从远程读记录并生成对应的实体类对象    下面的函数实现从远程数据库读写一个记录(条件由应用程序员指定),并将读取的记录转换成一个客户端实体类对象://
    // Export class function...
    class function TInvoices.ReadFromDB(NodeId: String; SBurro: TSyncBurro; Condition: string): TInvoices;
    var
      SqlCommand: string;
      Cds: TClientDataset;
    begin
      Result := nil;
      Cds:=TClientDataset.Create(nil);
      SqlCommand := 'SELECT * FROM [Invoices] WHERE '+condition;
      if not SBurro.ReadDataset(NodeId,SqlCommand,Cds) then
         begin
           Cds.Free;
           exit;
         end;
      Result:=ReadFromCds(Cds);
      Cds.Free;
    end;
    5、将一个实体类写入到远程数据库中    下面的函数实现将实体类对象写入到远程数据表中,即插入一个记录://
    // Insert to database method...
    function TInvoices.InsertToDB(NodeId: String; SBurro: TSyncBurro; var NewKeyValue: integer): boolean;
    var
      Cds: TClientDataset;
    begin
      Cds:=TInvoices.CreateCds;
      try
         Cds.Append;
         Cds.FieldValues['ShipName']:=ff_ShipName;
         Cds.FieldValues['ShipAddress']:=ff_ShipAddress;
         Cds.FieldValues['ShipCity']:=ff_ShipCity;
         Cds.FieldValues['ShipRegion']:=ff_ShipRegion;
         Cds.FieldValues['ShipPostalCode']:=ff_ShipPostalCode;
         Cds.FieldValues['ShipCountry']:=ff_ShipCountry;
         Cds.FieldValues['CustomerID']:=ff_CustomerID;
         Cds.FieldValues['CustomerName']:=ff_CustomerName;
         Cds.FieldValues['Address']:=ff_Address;
         Cds.FieldValues['City']:=ff_City;
         Cds.FieldValues['Region']:=ff_Region;
         Cds.FieldValues['PostalCode']:=ff_PostalCode;
         Cds.FieldValues['Country']:=ff_Country;
         Cds.FieldValues['Salesperson']:=ff_Salesperson;
         Cds.FieldValues['OrderID']:=ff_OrderID;
         Cds.FieldValues['OrderDate']:=ff_OrderDate;
         Cds.FieldValues['RequiredDate']:=ff_RequiredDate;
         Cds.FieldValues['ShippedDate']:=ff_ShippedDate;
         Cds.FieldValues['ShipperName']:=ff_ShipperName;
         Cds.FieldValues['ProductID']:=ff_ProductID;
         Cds.FieldValues['ProductName']:=ff_ProductName;
         Cds.FieldValues['UnitPrice']:=ff_UnitPrice;
         Cds.FieldValues['Quantity']:=ff_Quantity;
         Cds.FieldValues['Discount']:=ff_Discount;
         Cds.FieldValues['ExtendedPrice']:=ff_ExtendedPrice;
         Cds.FieldValues['Freight']:=ff_Freight;
         Cds.Post;
      except
         Cds.Free;
         Result:=false;
         exit;
      end;
      Result:=SBurro.AppendRecord(NodeId,'[Invoices]',Cds,NewKeyValue);
      Cds.Free;
    end;
    6、将实体类对象更新到远程数据表的记录中    下面的函数将一个实体类对象的属性作为记录数据写入到一个远程数据表记录中,目标记录定位条件等由应用程序员指定://
    // Update to database method...
    function TInvoices.UpdateToDB(NodeId: String; SBurro: TSyncBurro; Condition: String): boolean;
    var
      Cds: TClientDataset;
    begin
      Cds:=TInvoices.CreateCds;
      try
         Cds.Append;
         Cds.FieldValues['ShipName']:=ff_ShipName;
         Cds.FieldValues['ShipAddress']:=ff_ShipAddress;
         Cds.FieldValues['ShipCity']:=ff_ShipCity;
         Cds.FieldValues['ShipRegion']:=ff_ShipRegion;
         Cds.FieldValues['ShipPostalCode']:=ff_ShipPostalCode;
         Cds.FieldValues['ShipCountry']:=ff_ShipCountry;
         Cds.FieldValues['CustomerID']:=ff_CustomerID;
         Cds.FieldValues['CustomerName']:=ff_CustomerName;
         Cds.FieldValues['Address']:=ff_Address;
         Cds.FieldValues['City']:=ff_City;
         Cds.FieldValues['Region']:=ff_Region;
         Cds.FieldValues['PostalCode']:=ff_PostalCode;
         Cds.FieldValues['Country']:=ff_Country;
         Cds.FieldValues['Salesperson']:=ff_Salesperson;
         Cds.FieldValues['OrderID']:=ff_OrderID;
         Cds.FieldValues['OrderDate']:=ff_OrderDate;
         Cds.FieldValues['RequiredDate']:=ff_RequiredDate;
         Cds.FieldValues['ShippedDate']:=ff_ShippedDate;
         Cds.FieldValues['ShipperName']:=ff_ShipperName;
         Cds.FieldValues['ProductID']:=ff_ProductID;
         Cds.FieldValues['ProductName']:=ff_ProductName;
         Cds.FieldValues['UnitPrice']:=ff_UnitPrice;
         Cds.FieldValues['Quantity']:=ff_Quantity;
         Cds.FieldValues['Discount']:=ff_Discount;
         Cds.FieldValues['ExtendedPrice']:=ff_ExtendedPrice;
         Cds.FieldValues['Freight']:=ff_Freight;
         Cds.Post;
      except
         Cds.Free;
         Result:=false;
         exit;
      end;
      Result:=SBurro.ModifyRecord(NodeId,'[Invoices]',Condition,Cds);
      Cds.Free;
    end;
    7、在远程数据表中删除记录    删除的条件串由应用程序员指定。该方法是类方法,即不需要实际的实体对象,就可调用。//
    // Delete from database method...
    class function TInvoices.DeleteFromDB(NodeId: String; SBurro: TSyncBurro; Condition: string): boolean;
    var
       SqlCommand,tmpstr: string;
    begin
      SqlCommand:='DELETE FROM [Invoices] WHERE '+condition;
      result:=SBurro.ExecSQL(NodeId,SqlCommand,0,tmpstr);
    end; 
      

  2.   

    8、由一个远程数据集创建实体类列表对象    根据一个过滤条件得到一个远程数据集,然后将其转换成实体类列表对象(从TList派生),生成过程调用了实体类的生成函数://
    // Create a TInvoicesList instance from remote table...
    class function TInvoicesList.ReadFromDB(NodeId: String; SBurro: TSyncBurro; Condition: string; Orderby: string): TInvoicesList;
    var
      SqlCommand: string;
      Cds: TClientDataset;
      TmpObj: TInvoices;
    begin
      Result := TInvoicesList.Create;
      Cds:=TClientDataset.Create(nil);
      if Condition='' then
         begin
            SqlCommand := 'SELECT '
                        +'[ShipName],'
                        +'[ShipAddress],'
                        +'[ShipCity],'
                        +'[ShipRegion],'
                        +'[ShipPostalCode],'
                        +'[ShipCountry],'
                        +'[CustomerID],'
                        +'[CustomerName],'
                        +'[Address],'
                        +'[City],'
                        +'[Region],'
                        +'[PostalCode],'
                        +'[Country],'
                        +'[Salesperson],'
                        +'[OrderID],'
                        +'[OrderDate],'
                        +'[RequiredDate],'
                        +'[ShippedDate],'
                        +'[ShipperName],'
                        +'[ProductID],'
                        +'[ProductName],'
                        +'[UnitPrice],'
                        +'[Quantity],'
                        +'[Discount],'
                        +'[ExtendedPrice],'
                        +'[Freight]'
                        +' FROM [Invoices]'
         end
      else
         begin
            SqlCommand := 'SELECT '
                        +'[ShipName],'
                        +'[ShipAddress],'
                        +'[ShipCity],'
                        +'[ShipRegion],'
                        +'[ShipPostalCode],'
                        +'[ShipCountry],'
                        +'[CustomerID],'
                        +'[CustomerName],'
                        +'[Address],'
                        +'[City],'
                        +'[Region],'
                        +'[PostalCode],'
                        +'[Country],'
                        +'[Salesperson],'
                        +'[OrderID],'
                        +'[OrderDate],'
                        +'[RequiredDate],'
                        +'[ShippedDate],'
                        +'[ShipperName],'
                        +'[ProductID],'
                        +'[ProductName],'
                        +'[UnitPrice],'
                        +'[Quantity],'
                        +'[Discount],'
                        +'[ExtendedPrice],'
                        +'[Freight]'
                        +' FROM [Invoices] WHERE '+Condition;
         end;
      if Orderby<>'' then
         SqlCommand:=SqlCommand+' ORDER BY '+Orderby;
      if not SBurro.ReadDataset(NodeId,SqlCommand,Cds) then
         begin
            Cds.Free;
            exit;
         end;
      Cds.First;
      while not Cds.Eof do
         begin
            TmpObj := TInvoices.ReadFromCDS(Cds);
            Result.Add(TmpObj);
            Cds.Next;
         end;
      Cds.Free;
    end;
    9、根据一个Cds生成一个实体类列表对象    数据集已经存在于ClientDataset对象中,生成该数据集对应的实体类列表对象://
    // Create a TInvoicesList instance from CDS...
    class function TInvoicesList.ReadFromCDS(Cds: TClientDataset): TInvoicesList;
    var
      TmpObj: TInvoices;
    begin
      Result := TInvoicesList.Create;
      Cds.First;
      while not Cds.Eof do
         begin
            TmpObj := TInvoices.ReadFromCDS(Cds);
            Result.Add(TmpObj);
            Cds.Next;
         end;
    end;
    10、将实体类列表对象的内容写入ClientDataset容器    下面的函数将实体类对象列表的内容写入一个Cds中,该Cds原有内容将被清除。本方法常用于在dbgrid中显示远程数据集。//
    // Save to ClientDataset...
    function TInvoicesList.WriteToCds(aCds: TClientDataset): boolean;
    var
      TmpObj: TInvoices;
      i: integer;
      ok: boolean;
      Cds: TClientDataset;
    begin
      Cds:=TInvoices.CreateCds;
      ok:=true;
      for i:=0 to Self.Count-1 do
         begin
            TmpObj:=Self.items[i];
            try
               Cds.Append;
               Cds.FieldValues['ShipName']:=TmpObj.f_ShipName;
               Cds.FieldValues['ShipAddress']:=TmpObj.f_ShipAddress;
               Cds.FieldValues['ShipCity']:=TmpObj.f_ShipCity;
               Cds.FieldValues['ShipRegion']:=TmpObj.f_ShipRegion;
               Cds.FieldValues['ShipPostalCode']:=TmpObj.f_ShipPostalCode;
               Cds.FieldValues['ShipCountry']:=TmpObj.f_ShipCountry;
               Cds.FieldValues['CustomerID']:=TmpObj.f_CustomerID;
               Cds.FieldValues['CustomerName']:=TmpObj.f_CustomerName;
               Cds.FieldValues['Address']:=TmpObj.f_Address;
               Cds.FieldValues['City']:=TmpObj.f_City;
               Cds.FieldValues['Region']:=TmpObj.f_Region;
               Cds.FieldValues['PostalCode']:=TmpObj.f_PostalCode;
               Cds.FieldValues['Country']:=TmpObj.f_Country;
               Cds.FieldValues['Salesperson']:=TmpObj.f_Salesperson;
               Cds.FieldValues['OrderID']:=TmpObj.f_OrderID;
               Cds.FieldValues['OrderDate']:=TmpObj.f_OrderDate;
               Cds.FieldValues['RequiredDate']:=TmpObj.f_RequiredDate;
               Cds.FieldValues['ShippedDate']:=TmpObj.f_ShippedDate;
               Cds.FieldValues['ShipperName']:=TmpObj.f_ShipperName;
               Cds.FieldValues['ProductID']:=TmpObj.f_ProductID;
               Cds.FieldValues['ProductName']:=TmpObj.f_ProductName;
               Cds.FieldValues['UnitPrice']:=TmpObj.f_UnitPrice;
               Cds.FieldValues['Quantity']:=TmpObj.f_Quantity;
               Cds.FieldValues['Discount']:=TmpObj.f_Discount;
               Cds.FieldValues['ExtendedPrice']:=TmpObj.f_ExtendedPrice;
               Cds.FieldValues['Freight']:=TmpObj.f_Freight;
               Cds.Post;
            except
               ok:=false;
            end;
            if not ok then
               break;
         end;
      if ok then
         begin
            aCds.data:=Cds.Data;
            Result:=true;
         end
      else
         Result:=false;
      Cds.Free; 
    end;
      

  3.   

    11、将一个实体类列表对象内容写入远程数据表:    下面的函数将一个实体类对象列表中的数据(一个数据集)写入到远程数据表中,节点名、表名等由应用程序员指定://
    // Write to target table...
    function TInvoicesList.WriteToDB(NodeId: string; SBurro: TSyncBurro; TargetTable: string): boolean;
    var
      Cds: TClientDataset;
      TmpObj: TInvoices;
      i: integer;
      ok: boolean;
    begin
      Cds:=TInvoices.CreateCds;
      ok:=true;
      for i:=0 to Self.Count-1 do
         begin
            TmpObj:=Self.items[i];
            try
               Cds.Append;
               Cds.FieldValues['ShipName']:=TmpObj.f_ShipName;
               Cds.FieldValues['ShipAddress']:=TmpObj.f_ShipAddress;
               Cds.FieldValues['ShipCity']:=TmpObj.f_ShipCity;
               Cds.FieldValues['ShipRegion']:=TmpObj.f_ShipRegion;
               Cds.FieldValues['ShipPostalCode']:=TmpObj.f_ShipPostalCode;
               Cds.FieldValues['ShipCountry']:=TmpObj.f_ShipCountry;
               Cds.FieldValues['CustomerID']:=TmpObj.f_CustomerID;
               Cds.FieldValues['CustomerName']:=TmpObj.f_CustomerName;
               Cds.FieldValues['Address']:=TmpObj.f_Address;
               Cds.FieldValues['City']:=TmpObj.f_City;
               Cds.FieldValues['Region']:=TmpObj.f_Region;
               Cds.FieldValues['PostalCode']:=TmpObj.f_PostalCode;
               Cds.FieldValues['Country']:=TmpObj.f_Country;
               Cds.FieldValues['Salesperson']:=TmpObj.f_Salesperson;
               Cds.FieldValues['OrderID']:=TmpObj.f_OrderID;
               Cds.FieldValues['OrderDate']:=TmpObj.f_OrderDate;
               Cds.FieldValues['RequiredDate']:=TmpObj.f_RequiredDate;
               Cds.FieldValues['ShippedDate']:=TmpObj.f_ShippedDate;
               Cds.FieldValues['ShipperName']:=TmpObj.f_ShipperName;
               Cds.FieldValues['ProductID']:=TmpObj.f_ProductID;
               Cds.FieldValues['ProductName']:=TmpObj.f_ProductName;
               Cds.FieldValues['UnitPrice']:=TmpObj.f_UnitPrice;
               Cds.FieldValues['Quantity']:=TmpObj.f_Quantity;
               Cds.FieldValues['Discount']:=TmpObj.f_Discount;
               Cds.FieldValues['ExtendedPrice']:=TmpObj.f_ExtendedPrice;
               Cds.FieldValues['Freight']:=TmpObj.f_Freight;
               Cds.Post;
            except
               ok:=false;
            end;
            if not ok then
               break;
         end;
      if ok then
         ok:=SBurro.WriteClientDataset(NodeId,TargetTable,Cds);
      Cds.Free;
      result:=ok;
    end;
    12、实体类列表对象内容导出到文件    将实体类列表对象所表示的数据集导出到一个磁盘文件中,导出格式有三种:二进制、XML、UTF8XML://
    // Save data to a file...
    function TInvoicesList.SaveToFile(FileName: string; DataFormat: TDataPacketFormat): boolean;
    var
      Cds: TClientDataset;
      TmpObj: TInvoices;
      i: integer;
      ok: boolean;
    begin
      Cds:=TInvoices.CreateCds;
      ok:=true;
      for i:=0 to Self.Count-1 do
         begin
            TmpObj:=Self.items[i];
            try
               Cds.Append;
               Cds.FieldValues['ShipName']:=TmpObj.f_ShipName;
               Cds.FieldValues['ShipAddress']:=TmpObj.f_ShipAddress;
               Cds.FieldValues['ShipCity']:=TmpObj.f_ShipCity;
               Cds.FieldValues['ShipRegion']:=TmpObj.f_ShipRegion;
               Cds.FieldValues['ShipPostalCode']:=TmpObj.f_ShipPostalCode;
               Cds.FieldValues['ShipCountry']:=TmpObj.f_ShipCountry;
               Cds.FieldValues['CustomerID']:=TmpObj.f_CustomerID;
               Cds.FieldValues['CustomerName']:=TmpObj.f_CustomerName;
               Cds.FieldValues['Address']:=TmpObj.f_Address;
               Cds.FieldValues['City']:=TmpObj.f_City;
               Cds.FieldValues['Region']:=TmpObj.f_Region;
               Cds.FieldValues['PostalCode']:=TmpObj.f_PostalCode;
               Cds.FieldValues['Country']:=TmpObj.f_Country;
               Cds.FieldValues['Salesperson']:=TmpObj.f_Salesperson;
               Cds.FieldValues['OrderID']:=TmpObj.f_OrderID;
               Cds.FieldValues['OrderDate']:=TmpObj.f_OrderDate;
               Cds.FieldValues['RequiredDate']:=TmpObj.f_RequiredDate;
               Cds.FieldValues['ShippedDate']:=TmpObj.f_ShippedDate;
               Cds.FieldValues['ShipperName']:=TmpObj.f_ShipperName;
               Cds.FieldValues['ProductID']:=TmpObj.f_ProductID;
               Cds.FieldValues['ProductName']:=TmpObj.f_ProductName;
               Cds.FieldValues['UnitPrice']:=TmpObj.f_UnitPrice;
               Cds.FieldValues['Quantity']:=TmpObj.f_Quantity;
               Cds.FieldValues['Discount']:=TmpObj.f_Discount;
               Cds.FieldValues['ExtendedPrice']:=TmpObj.f_ExtendedPrice;
               Cds.FieldValues['Freight']:=TmpObj.f_Freight;
               Cds.Post;
            except
               ok:=false;
            end;
            if not ok then
               break;
         end;
      if ok then
         begin
            try
               Cds.SaveToFile(FileName,DataFormat);
            except
               ok:=false;
            end;
         end;
      Cds.Free;
      result:=ok;
    end;
    13、从文件中导入数据,生成实体类列表对象的各元素    上一函数的反过程,从磁盘文件导入数据,生成实体类列表对象://
    // Load data from a file...
    function TInvoicesList.LoadFromFile(FileName: string): boolean;
    var
      Cds: TClientDataset;
      TmpObj: TInvoices;
      ok: boolean;
    begin
      self.Clear;
      Cds:=TClientDataset.Create(nil);
      try
         Cds.LoadFromFile(FileName);
         ok:=Cds.Active;
      except
         ok:=false;
      end;
      if not ok then
         begin
            cds.Free;
            result:=false;
            exit;
         end;
      cds.First;
      while not cds.Eof do
         begin
            TmpObj := TInvoices.ReadFromCDS(Cds);
            Self.Add(TmpObj);
            Cds.Next;
         end;
      Cds.Free;
      result:=true;
    end;14、导出实体类列表对象的数据到流对象//
    // Save data to a memorystream...
    function TInvoicesList.SaveToStream(aStream: TMemoryStream; DataFormat: TDataPacketFormat): boolean;
    var
      Cds: TClientDataset;
      TmpObj: TInvoices;
      i: integer;
      ok: boolean;
    begin
      Cds:=TInvoices.CreateCds;
      ok:=true;
      for i:=0 to Self.Count-1 do
         begin
            TmpObj:=Self.items[i];
            try
               Cds.Append;
               Cds.FieldValues['ShipName']:=TmpObj.f_ShipName;
               Cds.FieldValues['ShipAddress']:=TmpObj.f_ShipAddress;
               Cds.FieldValues['ShipCity']:=TmpObj.f_ShipCity;
               Cds.FieldValues['ShipRegion']:=TmpObj.f_ShipRegion;
               Cds.FieldValues['ShipPostalCode']:=TmpObj.f_ShipPostalCode;
               Cds.FieldValues['ShipCountry']:=TmpObj.f_ShipCountry;
               Cds.FieldValues['CustomerID']:=TmpObj.f_CustomerID;
               Cds.FieldValues['CustomerName']:=TmpObj.f_CustomerName;
               Cds.FieldValues['Address']:=TmpObj.f_Address;
               Cds.FieldValues['City']:=TmpObj.f_City;
               Cds.FieldValues['Region']:=TmpObj.f_Region;
               Cds.FieldValues['PostalCode']:=TmpObj.f_PostalCode;
               Cds.FieldValues['Country']:=TmpObj.f_Country;
               Cds.FieldValues['Salesperson']:=TmpObj.f_Salesperson;
               Cds.FieldValues['OrderID']:=TmpObj.f_OrderID;
               Cds.FieldValues['OrderDate']:=TmpObj.f_OrderDate;
               Cds.FieldValues['RequiredDate']:=TmpObj.f_RequiredDate;
               Cds.FieldValues['ShippedDate']:=TmpObj.f_ShippedDate;
               Cds.FieldValues['ShipperName']:=TmpObj.f_ShipperName;
               Cds.FieldValues['ProductID']:=TmpObj.f_ProductID;
               Cds.FieldValues['ProductName']:=TmpObj.f_ProductName;
               Cds.FieldValues['UnitPrice']:=TmpObj.f_UnitPrice;
               Cds.FieldValues['Quantity']:=TmpObj.f_Quantity;
               Cds.FieldValues['Discount']:=TmpObj.f_Discount;
               Cds.FieldValues['ExtendedPrice']:=TmpObj.f_ExtendedPrice;
               Cds.FieldValues['Freight']:=TmpObj.f_Freight;
               Cds.Post;
            except
               ok:=false;
            end;
            if not ok then
               break;
         end;
      if ok then
         begin
            try
               aStream.Clear;
               Cds.SaveToStream(aStream,DataFormat);
            except
               ok:=false;
            end;
         end;
      Cds.Free;
      result:=ok;
    end;