创建与 TDataSet 关联的持久字段组件的代码

发布于 2024-11-17 22:16:02 字数 150 浏览 0 评论 0原文

寻找一些示例代码,说明如何在运行时创建与数据集关联的 TField 组件。

在 IDE 中,如果您放下数据集组件,右键单击会弹出字段编辑器,该编辑器在设计时提供此功能。无法找到显示如何在运行时执行此操作的代码。

TIA

Looking for some example code illustrating how to create at runtime TField Components associated with a dataset.

In the IDE, if you plop down a dataset component, right-clicking brings up the fields editor that provides this functionality at design time. Have not been able to find code showing how to do it at runtime.

TIA

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

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

发布评论

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

评论(2

青春如此纠结 2024-11-24 22:16:02

每个字段类型都有一个 Create 函数,您将 DataSet 传递给该函数,该函数将创建该类型的字段并将其添加到 Fields 中。来自 DB.TStringField.Create 的帮助。

var
  T: TStringField;
begin
  SQLDataSet1.Close;
  T := TStringField.Create(SQLDataSet1);
  T.FieldName := 'LastName';
  T.Name := SQLDataSet1.Name + T.FieldName;
  T.Index := SQLDataSet1.FieldCount;
  T.DataSet := SQLDataSet1;
  SQLDataSet1.FieldDefs.UpDate;
  SQLDataSet1.Open;
end;

Each field type has a Create function that you pass the DataSet to that creates a field of that type and adds it to the Fields. From the help for DB.TStringField.Create.

var
  T: TStringField;
begin
  SQLDataSet1.Close;
  T := TStringField.Create(SQLDataSet1);
  T.FieldName := 'LastName';
  T.Name := SQLDataSet1.Name + T.FieldName;
  T.Index := SQLDataSet1.FieldCount;
  T.DataSet := SQLDataSet1;
  SQLDataSet1.FieldDefs.UpDate;
  SQLDataSet1.Open;
end;
国粹 2024-11-24 22:16:02

在运行时创建的“持久字段”没有任何意义。在 IDE 中创建持久字段允许将它们写入表单/数据模块的 .dfm,然后在从可执行文件加载 .dfm 时自动创建它们,并且可以通过使用该数据模块的代码中的名称进行访问。

如果您希望在运行时不必使用 FieldByName,则可以执行以下操作:

TMyDataModule=class(TDataModule)
  // Usual IDE created stuff, etc.
public
  NameFld: TStringField;
  LimitFld: TFloatField;
end;

procedure TMyDataModule.DataModuleCreate(Sender: TObject);    
begin
  NameFld := MyDataSet.FieldByName('CompanyName') as TStringField;
  NameFld.Required := True;
  LimitFld := MyDataSet.FieldByName('CreditLimit') as TFloatField;
  LimitFld.Currency := True;
  // Set other properties as needed.
end;

您现在在运行时拥有相当于持久字段的功能。可以像往常一样在使用数据模块的其他代码中访问它们。

procedure TMyDataModule.DoSomethingWithData(CompanyName: string; CreditLimit: Currency);
begin
  MyDataSet.Edit;
  NameFld.AsString := CompanyName;
  LimitFld.AsCurrency := CreditLimit;
  MyDataSet.Post;
end;

编辑:只是想到了我的陈述“没有意义”的两个例外 - 那些将是计算字段或查找字段。对于查找字段,最简单的方法是通过 JOIN 将它们添加到 SQL 中,然后让服务器执行此操作;对于计算字段,您可以使用DataSet.FieldDefs.Add并设置适当的属性来命名字段,将新创建字段的FieldType设置为ftCalculated code>,并分配一个 OnCalcFields 事件处理程序来处理计算。

"Persistent fields" created at runtime make no sense. Creating persistent fields in the IDE allows them to be written to the .dfm for the form/datamodule, and then be created automatically when that .dfm is loaded from the executable, and can be accessed by name in your code that uses that datamodule.

If you're looking to not have to use FieldByName at runtime, you can just do something like this:

TMyDataModule=class(TDataModule)
  // Usual IDE created stuff, etc.
public
  NameFld: TStringField;
  LimitFld: TFloatField;
end;

procedure TMyDataModule.DataModuleCreate(Sender: TObject);    
begin
  NameFld := MyDataSet.FieldByName('CompanyName') as TStringField;
  NameFld.Required := True;
  LimitFld := MyDataSet.FieldByName('CreditLimit') as TFloatField;
  LimitFld.Currency := True;
  // Set other properties as needed.
end;

You now have the equivalent of persistent fields at runtime. They can be accessed as usual in other code that uses your datamodule.

procedure TMyDataModule.DoSomethingWithData(CompanyName: string; CreditLimit: Currency);
begin
  MyDataSet.Edit;
  NameFld.AsString := CompanyName;
  LimitFld.AsCurrency := CreditLimit;
  MyDataSet.Post;
end;

EDIT: Just thought of two exceptions to my statement "make no sense" - those would be calculated or lookup fields. For lookup fields, it's easiest to just add them to SQL via a JOIN and let the server do it; for calculated fields, you can use the DataSet.FieldDefs.Add and set the appropriate properties to name the field, set the newly created field's FieldType to ftCalculated, and assign an OnCalcFields event handler to handle the calculation.

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