我试图理解如何用tfdstoredproc(firedac组件)填充tiwdbgrid(intraweb组件)

yh2wf1be  于 2021-06-19  发布在  Mysql
关注(0)|答案(1)|浏览(380)

目前,我有一个表单可以捕获登录数据,一个带有tiwdbgrid的表单应该返回与从mysql数据库登录时创建的用户id相关联的所有主机,还有一个共享数据模块。
下面是我的登录页面代码

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.

我的主机查找页有一个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.

我确保datasource选项设置为email\u data\u datamodule.hosts\u table\u requested\u 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.

如何确保将数据从主机\u请求的\u frm\u用户\u idproc填充到我的iwdbgrid?
提前谢谢,很抱歉邮件太长

yvt65v4c

yvt65v4c1#

您的应用程序有几个问题:
首先,所有表单都需要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子句(在interface部分中)添加声明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.

现在您所需要做的就是构建和运行应用程序。应该有用。
注意:上面所有的代码都没有经过测试(我是用记事本写的,不是用delphiide写的),所以可能会有打字错误和其他一些错误。

相关问题