博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
内存表 FDMemTable ClientDataSet CreateDataSet 动态创建字段
阅读量:6352 次
发布时间:2019-06-22

本文共 6089 字,大约阅读时间需要 20 分钟。

https://community.embarcadero.com/index.php/blogs/entry/firedac-in-memory-dataset-tfdmemtable

ClientDataSet FieldDefs Add Field CreateDataSet 动态创建字段

FDMemTable1.Close();  FDMemTable1.FieldDefs.Clear();  FDMemTable1.FieldDefs.Add('ID', ftInteger, 0, True);  FDMemTable1.FieldDefs.Add('Name', ftString, 20, false);  FDMemTable1.CreateDataSet();  FDMemTable1.AppendRecord([101, 'aaa']);  FDMemTable1.AppendRecord([102, 'bbb']);  FDMemTable1.AppendRecord([103, 'ccc']);

  TFDMemTable *table= new TFDMemTable();

delete table;

 

void __fastcall TForm1::createField(){  ClientDataSet1->Close();  ClientDataSet1->Fields->Clear();  ClientDataSet1->FieldDefs->Clear();  ClientDataSet1->Open();  ClientDataSet1->Close();  for(int i = 0 ;i < ClientDataSet1->Fields->Count ;i++)   ClientDataSet1->Fields->Fields[0]->Free();//释放所有的静态字段   for(int i = 0 ;i < ClientDataSet1->FieldDefs->Count ;i++)    ClientDataSet1->FieldDefs->Items[i]->CreateField(ClientDataSet1);  TBooleanField *NewField;   NewField = new TBooleanField(ClientDataSet1);  NewField->FieldName="SelectMe";  NewField->DisplayLabel = "选择";  NewField->FieldKind=fkInternalCalc;  NewField->DataSet=ClientDataSet1;  NewField->Index = 0;  ClientDataSet1->Open();} void __fastcall TForm1::BitBtn1Click(TObject *Sender) {
        sql = "select  from TABLE1 ";         this->ADOQuery1->Close();         this->ADOQuery1->SQL->Clear();         this->ADOQuery1->SQL->Add(this->sql);         this->ADOQuery1->Open();         this->createField(); }

 

cds1->Close();  cds1->FieldDefs->Clear();  cds1->FieldDefs->Add("ID", ftInteger, 0, true);  cds1->FieldDefs->Add("Name", ftString, 20, false);  cds1->FieldDefs->Add("DT", ftDateTime, 0, false);  cds1->CreateDataSet();    cds1->AppendRecord(  ARRAYOFCONST((101,"aaa",Now().DateTimeString() )));  cds1->AppendRecord(  ARRAYOFCONST((102,"bbb",Now().DateTimeString() )));  cds1->AppendRecord(  ARRAYOFCONST((103,"ccc",Now().DateTimeString() )));
ClientDataSet1.Close();  ClientDataSet1.FieldDefs.Clear();  ClientDataSet1.FieldDefs.Add('ID', ftInteger, 0, true);  ClientDataSet1.FieldDefs.Add('Name', ftString, 20, false);  ClientDataSet1.FieldDefs.Add('DT', ftDateTime, 0, false);  ClientDataSet1.FieldDefs.Add('chk', ftBoolean, 0, false);  ClientDataSet1.CreateDataSet();  ClientDataSet1.Open;  ClientDataSet1.Append;  ClientDataSet1.FieldByName('id').Value := 101;  ClientDataSet1.FieldByName('name').Value := 'abc';  ClientDataSet1.FieldByName('dt').Value := Now;  ClientDataSet1.Post;  ClientDataSet1.AppendRecord([102,'bbb',Now()]);  ClientDataSet1.AppendRecord([103,'ccc',Now()]);

 

CreateDataSet
FDMemTable1->Close();FDMemTable1->FieldDefs->Clear();FDMemTable1->FieldDefs->Add("ID", ftInteger, 0, true);FDMemTable1->FieldDefs->Add("Name", ftString, 20, false);FDMemTable1->FieldDefs->Add("DT", ftDateTime, 0, false);FDMemTable1->CreateDataSet();FDMemTable1->AppendRecord(ARRAYOFCONST((101, "aaa", Now().DateTimeString())));FDMemTable1->AppendRecord(ARRAYOFCONST((102, "bbb", Now().DateTimeString())));FDMemTable1->AppendRecord(ARRAYOFCONST((103, "ccc", Now().DateTimeString())));FDMemTable1->First();

 AddFieldDef

with CDS2 do  begin    with FieldDefs.AddFieldDef do    begin      DataType := ftInteger;      Name := 'Field1';    end;    with FieldDefs.AddFieldDef do    begin      DataType := ftString;      Size := 10;      Name := 'Field2';    end;    with IndexDefs.AddIndexDef do    begin      Fields := 'Field1';      Name := 'IntIndex';    end;    CreateDataSet;

 

C++也可以这样写

TVarRec row[] = {user, password, purview, "", 0};

 int value_size =8;
  qry->AppendRecord(row, value_size);

 

 

__fastcall TForm1::TForm1(TComponent* Owner)    : TForm(Owner){  TFieldDefs *pDefs = CDS2->FieldDefs;  TFieldDef *pDef = pDefs->AddFieldDef();  pDef->DataType = ftInteger;  pDef->Name = "Field1";   pDef = pDefs->AddFieldDef();  pDef->DataType = ftString;  pDef->Size = 10;  pDef->Name = "Field2";   TIndexDef *pIDef = CDS2->IndexDefs->AddIndexDef();  pIDef->Fields = "Field1";  pIDef->Name = "IntIndex";   CDS2->CreateDataSet();}

 

2016.8.25  FDMemTable 试验成功 ,三种创建的方式都最终成了InternalCalcField字段。

ClientDataSet1->Close();    ClientDataSet1->Fields->Clear();    ClientDataSet1->FieldDefs->Clear();    TFieldDef *fd;    fd = ClientDataSet1->FieldDefs->AddFieldDef();    fd->InternalCalcField = true;    fd->Name = "calc1";    fd->DataType = ftString;    fd->Size = 10;    fd = ClientDataSet1->FieldDefs->AddFieldDef();    fd->InternalCalcField = true;    fd->Name = "flag";    fd->DataType = ftBoolean;    ClientDataSet1->FieldDefs->Add("ID", ftString, 0, false);    ClientDataSet1->FieldDefs->Add("Name", ftString, 20, false);    ClientDataSet1->FieldDefs->Add("DT", ftString, 0, false);    ClientDataSet1->FieldDefs->Add("my", ftString, 0, false);    ClientDataSet1->FieldDefs->Find("my")->InternalCalcField = true;     for(int i = 0 ;i < ClientDataSet1->FieldDefs->Count ;i++)    ClientDataSet1->FieldDefs->Items[i]->CreateField(ClientDataSet1);      TBooleanField *NewField;  NewField = new TBooleanField(ClientDataSet1);  NewField->FieldName="SelectMe";  NewField->DisplayLabel = "选择";  NewField->FieldKind=fkInternalCalc;  NewField->DataSet=ClientDataSet1;//    ClientDataSet1->CreateDataSet(); 有了上面的CreateField,此句可不要。仅此句还不能创建fkInternalCalc字段,创建变成了fkData    ClientDataSet1->Open();    for (int i = 0; i < ClientDataSet1->Fields->Count; i++)    {        ClientDataSet1->Fields->Fields[i]->FieldName;        ClientDataSet1->Fields->Fields[i]->FieldKind;    }

 如果dfm窗体文件定义了字段,那么不用createDataSet了,只要open就可以了。

 object ds1: TFDMemTable

    OnCalcFields = ds1CalcFields
    FieldDefs = <>
    CachedUpdates = True
    IndexDefs = <>
    FetchOptions.AssignedValues = [evMode]
    FetchOptions.Mode = fmAll
    ResourceOptions.AssignedValues = [rvSilentMode]
    ResourceOptions.SilentMode = True
    UpdateOptions.AssignedValues = [uvCheckRequired, uvAutoCommitUpdates]
    UpdateOptions.CheckRequired = False
    UpdateOptions.AutoCommitUpdates = True
    StoreDefs = True
    Left = 352
    Top = 160
    object ds1Field: TDateTimeField
      FieldName = aaa
    end
    object ds1Field2: TStringField
      FieldName = bbb
    end
    object ds1Field3: TStringField
      FieldName = ccc
    end

ds1->Close();

ds1->Open();

转载地址:http://dcmla.baihongyu.com/

你可能感兴趣的文章
【实用】面对枯燥的源码,如何才能看得下去?
查看>>
智库说 | 徐远:数字时代的城市潜力
查看>>
《JSP极简教程》jsp c:forEach用法
查看>>
WebSocket详解(六):刨根问底WebSocket与Socket的关系
查看>>
用 Go 写一个轻量级的 ssh 批量操作工具
查看>>
网站设计之合理架构CSS 架构CSS
查看>>
OTP 22.0 RC3 发布,Erlang 编写的应用服务器
查看>>
D语言/DLang 2.085.1 发布,修复性迭代
查看>>
感觉JVM的默认异常处理不够好,既然不好那我们就自己来处理异常呗!那么如何自己处理异常呢?...
查看>>
Java 基础 之 算数运算符
查看>>
Windows下配置安装Git(二)
查看>>
一个最简单的基于Android SearchView的搜索框
查看>>
铁路开通WiFi“钱景”不明
查看>>
Nutanix领衔的超融合能带来新存储黄金时代吗?
查看>>
Facebook申请专利 或让好友及陌生人相互拼车
查看>>
电力“十三五”规划:地面光伏与分布式的分水岭
查看>>
美联社再告FBI:要求公开请黑客解锁iPhone花费
查看>>
三星电子出售希捷和夏普等四家公司股份
查看>>
任志远:当云计算遇上混合云
查看>>
思科联手发那科 用物联网技术打造无人工厂
查看>>