在delphi中使用datamodules分开数据集实例 [英] separate dataset instances using datamodules in delphi

查看:179
本文介绍了在delphi中使用datamodules分开数据集实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Delphi6 并拥有一个数据模块,其中包含一个 ADO DataSet 由formA和FormB两种形式使用。每个表单在 OnCreate Dataset.Close() c中的 Dataset.Open() / code>在 OnClose 。如果两个表单同时打开,并且formB被关闭,则数据集在formA中关闭。我如何防止这种情况,本质上我需要为每种形式的数据集单独的实例,但同时使用数据模块。

I am using Delphi6 and have a data module with an ADO DataSet which is used by two forms, formA and FormB. Each form has a Dataset.Open() in OnCreate and Dataset.Close() in OnClose. If both forms are open simultaneously and formB is closed the dataset is closed in formA. How can I prevent this, essentially I need separate instances of the dataset for each form but at the same time use the datamodule.

推荐答案

实现你想要的最简单的方法是为每个表单创建一个数据模块的实例,并将其传递给表单,以便在表单关闭时可以被释放:

The simplest way to achieve what you want is to create an instance of the data module for each form, and pass it to the form so it can be freed when the form is closed:

var
  Data: TDataModule;
begin
  Data := T<YourDataModule>.Create(Self);
  try
    Form := T<YourForm>.Create(Self);
    Form.DataModule := Data;
    Data.Name := '';
  except
    Data.Free;
    raise;
  end;

  Form.Show;
end;

将DataModule的名称设置为空字符串,以确保VCL挂起数据的逻辑使用新创建的实例,而不是第一个实例来控制它们的数据源/数据集。

Setting the DataModule's Name to an empty string is done to ensure that the VCL's logic for hooking up data aware controls to their datasource/dataset is done using the newly created instance, instead of the first ever instance.

在Form的OnClose处理程序(或其析构函数)中,确保释放数据模块。

In the Form's OnClose handler (or its destructor) make sure to free the data module.

这篇关于在delphi中使用datamodules分开数据集实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆