我试图了解如何使用 TFDStoredProc(FireDac 组件)填充 TIWDBGrid(IntraWeb 组件)

I am trying to understand how to populate a TIWDBGrid(IntraWeb component) with a TFDStoredProc(FireDac component)

目前我有一个捕获登录数据的表单,一个带有 TIWDBGrid 的表单应该 return 与 user_id 关联的任何主机,当我从 mysql数据库,共享数据模块。

下面是我的登录页面代码

unit login_unit;

interface

uses
  Classes, SysUtils, IWAppForm, IWApplication, IWColor, IWTypes, IWCompButton,
  IWCompLabel, Vcl.Controls, IWVCLBaseControl, IWBaseControl, IWBaseHTMLControl,
  IWControl, IWCompEdit;

type
  Tlogin_form = class(TIWAppForm)
    enter_usermame_TIWEdit: TIWEdit;
    enter_password_TIWEdit: TIWEdit;
    Username: TIWLabel;
    Password: TIWLabel;
    login_TIWButton: TIWButton;
    returned_user_id_TIWEdit: TIWEdit;
    procedure login_TIWButtonClick(Sender: TObject);
  public
  end;

implementation

{$R *.dfm}

uses email_data, host_lookup_unit;


procedure Tlogin_form.login_TIWButtonClick(Sender: TObject);
var
  host_lookup_form:Thost_lookup_Form;
begin
  email_data_DataModule.Login_userProc.Prepare;
  email_data_DataModule.Login_userProc.ParamByName('user_name_').Value := enter_usermame_TIWEdit.Text;
  email_data_DataModule.Login_userProc.ParamByName('pass_word_').Value := enter_password_TIWEdit.Text;
  email_data_DataModule.Login_userProc.Execute;
  email_data_DataModule._user_id := email_data_DataModule.Login_userProc.ParamByName('user_id_').Value;
  returned_user_id_TIWEdit.Text := email_data_DataModule.Login_userProc.ParamByName('user_id_').Value;


  email_data_DataModule.Hosts_requested_frm_user_idProc.Prepare;
  email_data_DataModule.Hosts_requested_frm_user_idProc.ParamByName('user_id_').Value := email_data_DataModule._user_id;
  email_data_DataModule.Hosts_requested_frm_user_idProc.Execute;

  thost_lookup_form.Create(Self).Show;

end;

initialization
  Tlogin_form.SetAsMainForm;

end.

我的 host_look up 页面有一个 TIWDBGrid 组件,如下所示:

unit host_lookup_unit;

interface

uses
  Classes, SysUtils, IWAppForm, IWApplication, IWColor, IWTypes, Vcl.Controls,
  IWVCLBaseControl, IWBaseControl, IWBaseHTMLControl, IWControl, IWCompGrids,
  IWDBGrids, IWCompButton;

type
  Thost_lookup_Form = class(TIWAppForm)
    IWDBGrid1: TIWDBGrid;

  public
  end;

implementation

{$R *.dfm}

uses email_data;


end.

我确保数据源选项设置为 email_data_DataModule.hosts_table_requested_TDataSource

最后我的数据模块的代码如下:

unit email_data;

interface

uses
  Forms,
  SysUtils, Classes, FireDAC.Stan.Intf, FireDAC.Stan.Option, FireDAC.Stan.Error,
  FireDAC.UI.Intf, FireDAC.Phys.Intf, FireDAC.Stan.Def, FireDAC.Stan.Pool,
  FireDAC.Stan.Async, FireDAC.Phys, FireDAC.Phys.MySQL, FireDAC.Phys.MySQLDef,
  FireDAC.VCLUI.Wait, FireDAC.Stan.Param, FireDAC.DatS, FireDAC.DApt.Intf,
  FireDAC.DApt, Data.DB, FireDAC.Comp.DataSet, FireDAC.Comp.Client;

type
  Temail_data_DataModule = class(TDataModule)
    EmaildbConnection: TFDConnection;
    Login_userProc: TFDStoredProc;
    hosts_table_requested_TDataSource: TDataSource;
    Hosts_requested_frm_user_idProc: TFDStoredProc;
  private
  public
    _user_id : integer;
  end;

function email_data_DataModule:Temail_data_DataModule;

implementation

{$R *.dfm}

uses ServerController;
function email_data_DataModule:Temail_data_DataModule;
begin
  result := UserSession.email_data_DataModule;
end;

end.

如何确保将来自 Hosts_requested_frm_user_idProc 的数据填充到我的 IWDBGrid?

提前致谢,抱歉拖了这么久post

您的应用程序存在一些问题:

首先,所有表单都需要将 WebApplication 对象作为所有者,因此将创建 IWForm 的行更改为

procedure Tlogin_form.login_TIWButtonClick(Sender: TObject);
var
  host_lookup_form:Thost_lookup_Form;
begin
  ...
  thost_lookup_form.Create(WebApplication).Show;
end;

其次,我认为您不应该使用存储过程来检索数据,而应该使用查询对象。如果您使用的是 FireDAC,请考虑将其更改为将检索结果集的 TFDQuery。

此外,您必须将 TDataSource 放在与 IWDBGrid 相同的表单中,并通过 IWDBGrid.DataSource 属性.

连接它们

在IWForm(接口部分)的uses子句中添加DataModule声明的单元,并声明一个表单的字段。所以你应该有这样的东西:

unit host_lookup_unit;

interface

uses
  Classes, SysUtils, IWAppForm, IWApplication, IWColor, IWTypes, Vcl.Controls,
  IWVCLBaseControl, IWBaseControl, IWBaseHTMLControl, IWControl, IWCompGrids,
  IWDBGrids, IWCompButton,
  email_data;  // <- Include your DM here

type
  Thost_lookup_Form = class(TIWAppForm)
    IWDBGrid1: TIWDBGrid;
    hosts_table_requested_TDataSource: TDataSource;
  private
    FDataModule: Temail_data_DataModule;
  public
  end;

implementation

{$R *.dfm}

procedure Thost_lookup_Form .host_lookup_FormCreate(Sender: TObject);
begin
  FDataModule := email_data_DataModule;   // set your field referencing the DM here and use it within your form. Don't use the email_data_DataModule() function anymore!
  hosts_table_requested_TDataSource.DataSet :=  FDataModule.Login_userQuery;  // Connect your DataSource and your DataSet, via code. This is the best way to do it!
end;

我还强烈建议您删除处理设置参数和打开表单查询的逻辑。此代码属于 DataModule!

你应该有这样的东西:

unit email_data;

interface

uses
  Forms,
  SysUtils, Classes, FireDAC.Stan.Intf, FireDAC.Stan.Option, FireDAC.Stan.Error,
  FireDAC.UI.Intf, FireDAC.Phys.Intf, FireDAC.Stan.Def, FireDAC.Stan.Pool,
  FireDAC.Stan.Async, FireDAC.Phys, FireDAC.Phys.MySQL, FireDAC.Phys.MySQLDef,
  FireDAC.VCLUI.Wait, FireDAC.Stan.Param, FireDAC.DatS, FireDAC.DApt.Intf,
  FireDAC.DApt, Data.DB, FireDAC.Comp.DataSet, FireDAC.Comp.Client;

type
  Temail_data_DataModule = class(TDataModule)
    EmaildbConnection: TFDConnection;
    Login_userQuery: TFDQuery;
    Hosts_requested_frm_user_idQuery: TFDQuery;
  private
    F_user_id: Integer;
  public
    function SetUserNameAndPassword(const AUserName, AUserPassword: string): Integer;
  end;

function email_data_DataModule:Temail_data_DataModule;

implementation

{$R *.dfm}

uses ServerController;

function email_data_DataModule: Temail_data_DataModule;
begin
  result := UserSession.email_data_DataModule;
end;

function Temail_data_DataModule.SetUserNameAndPassword(const AUserName, AUserPassword: string): Integer;
begin
  Login_userQuery.Prepare;
  Login_userQuery.ParamByName('user_name_').Value := AUserName;
  Login_userQuery.ParamByName('pass_word_').Value := AUserPassword;
  Login_userQuery.Open;
  F_user_id := Login_userQuery.ParamByName('user_id_').Value;
  Result := Login_userQuery.ParamByName('user_id_').Value;

  Hosts_requested_frm_user_idQuery.Prepare;
  Hosts_requested_frm_user_idQuery.ParamByName('user_id_').Value := F_user_id;
  Hosts_requested_frm_user_idQuery.Open;  
end;

end.

您现在需要做的就是构建和 运行 应用程序。它应该有效。

注意:以上所有代码都未经测试(我是用记事本写的,不是 Delphi IDE)所以可能有拼写错误和其他一些错误。