gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-10-30 23:28:38 35 4
gpt4 key购买 nike

目前我有一个捕获登录数据的表单,一个带有 TIWDBGrid 的表单,它应该返回与我从我的 mysql 数据库登录时创建的 user_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.

我的 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.

我确保将 DataSource 选项设置为 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?

提前致谢,抱歉发了这么长的帖子

最佳答案

您的申请存在一些问题:

首先,您所有的表单都需要将 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 属性连接它们。

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

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 中编写的)因此可能存在拼写错误和其他一些错误。

关于mysql - 我试图了解如何使用 TFDStoredProc(FireDac 组件)填充 TIWDBGrid(IntraWeb 组件),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52600711/

35 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com