我正在尝试创建一个包含整个查询(CRUD)的类,以供其他单位/表单调用和使用它。但是后来我遇到了一些错误。 我正在使用Firebird作为数据库。
我不知道如何添加查询以在函数内执行。 尝试在函数中输入查询代码时出现错误。
___success;
但是如果我在按钮触发过程中输入查询代码,就不会有错误。
unit AllClass;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants,
System.Classes, Vcl.Graphics, Vcl.Controls, Vcl.Forms, Vcl.Dialogs,
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.FB,FireDAC.Phys.FBDef, FireDAC.VCLUI.Wait,
FireDAC.Stan.Param, FireDAC.DatS,FireDAC.DApt.Intf, FireDAC.DApt, Data.DB,
FireDAC.Comp.DataSet,FireDAC.Comp.Client, Vcl.StdCtrls;
type
TForm6 = class(TForm)
FDConnection1: TFDConnection;
EveryQuery: TFDQuery;
procedure login(username,password:String);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form6: TForm6;
implementation
procedure login(username,password:String);
begin
EveryQuery.SQL.Clear; //here error
end;
end.
如果您对此问题有任何解决方案或想法,请帮助我。
答案 0 :(得分:2)
您的function login
应该是function TForm6.Login
,因为它被声明为表单本身的方法。您可以通过在表单类中(在私有或公共部分中,而不是在当前位置的地方)编写声明并单击 Ctrl + Shift + < kbd> C ,IDE将在implementation
部分生成正确的代码。
您当前的代码:
procedure login(username,password:String);
begin
EveryQuery.SQL.Clear; //here error
end;
更正的代码:
procedure TForm6.login(username,password:String);
begin
EveryQuery.SQL.Clear; //here error
end;
答案 1 :(得分:2)
您的login()
过程被声明为TForm6
类的成员,但是您不是实现作为类成员。您需要在TForm6.
部分的login
过程名称前面添加implementation
,例如:
unit AllClass;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants,
System.Classes, Vcl.Graphics, Vcl.Controls, Vcl.Forms, Vcl.Dialogs,
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.FB,FireDAC.Phys.FBDef, FireDAC.VCLUI.Wait,
FireDAC.Stan.Param, FireDAC.DatS,FireDAC.DApt.Intf, FireDAC.DApt, Data.DB,
FireDAC.Comp.DataSet,FireDAC.Comp.Client, Vcl.StdCtrls;
type
TForm6 = class(TForm)
FDConnection1: TFDConnection;
EveryQuery: TFDQuery;
private
{ Private declarations }
public
{ Public declarations }
function login(username, password: String): boolean;
end;
var
Form6: TForm6;
implementation
{$R *.dfm}
function TForm6.login(username, password: String): boolean;
begin
Result := False;
if (username <> '') and (password <> '') then
begin
//use parameter method
EveryQuery.Close;
EveryQuery.SQL.Text := 'Select 1 from MYGUESTS where FIRSTNAME = :theID and Password = :thePsw';
EveryQuery.ParamByName('theID').AsString := username;
EveryQuery.ParamByName('thePsw').AsString := password;
EveryQuery.Open;
Result := not EveryQuery.Eof;
EveryQuery.Close;
if not Result then
ShowMessage('Wrong username or password. Please re-enter');
end else
begin
ShowMessage('Please enter something');
end;
end;
end.
然后您可以这样称呼它:
procedure TForm1.LogInClick(Sender: TObject);
begin
if Form6.login(Username.Text, Password.Text) then
begin
Username.Clear;
Password.Clear;
Form3.Show;
Hide;
end;
end;