DELPHI - Access Violation e erro com FireDAC.Client

28/11/2020

0

Pessoal, estou com um problema ao fazer o cadastro de aluno na minha aplicação. Quando o botão de cadastro é pressionado, dá um erro em FireDAC.client. Segue abaixo as units DAO do aluno, o formulario de cadastro, e a uDm.



uses uAluno, uBaseDAO, System.SysUtils, System.Generics.Collections,
  FireDAC.Comp.Client, FireDAC.Dapt, uDM, Data.DB;



type
  TAlunoDAO = class(TBaseDAO)



  private
    FListarAluno: TObjectList<TAluno>;
    procedure ListaAluno(Ds: TFDQuery);



  public
    constructor Create;
    destructor Destroy;
    function InserirAluno(pAluno: TAluno): Boolean;
    function AlterarAluno(pAluno: TAluno): Boolean;
    function ExcluirAluno(pAluno: TAluno): Boolean;
    function VerificarLogin(Nome, Senha: string): Boolean;
    function RecuperaNome(user: string): String;
    function BuscaAluno: TObjectList <TAluno>;



  end;



implementation



{ TAlunoDAO }



function TAlunoDAO.AlterarAluno(pAluno: TAluno): Boolean;
var
  SQL: string;
  Aluno: TAluno;
begin
  SQL := 'UPDATE Aluno set Nome_Completo = '
  + QuotedStr(pAluno.NomeCompleto) + ', ' +
    'data_nascimento = ' +
    QuotedStr(FormatDateTime('yyyy-mm-dd,', paluno.DataNasc)) + ', '
    + 'senha = ' +
    QuotedStr(pAluno.senha) +
    'WHERE id_Aluno = ' + IntToStr(Aluno.Id_Aluno);
  Result := ExecutarComando(SQL) > 0;
end;



function TAlunoDAO.BuscaAluno: TObjectList<TAluno>;
var
  SQL: String;
begin
  SQL := 'SELECT * FROM Aluno';
  FQuery := RetornarDataSet(SQL);



  if not(FQuery.isEmpty) then
  begin
    ListaAluno(FQuery);
    Result := FListarAluno;
  end;



end;



constructor TAlunoDAO.Create;
begin
  FListarAluno := TObjectList<TAluno>.Create;
end;



destructor TAlunoDAO.Destroy;
begin
  try
    inherited;
    if Assigned(FListarAluno) then
      FreeAndNil(FListarAluno);
  except
    on e: exception do
      raise exception.Create(e.Message);
  end;
end;



function TAlunoDAO.ExcluirAluno(pAluno: TAluno): Boolean;
var
  SQL: string;
  Aluno: TAluno;
begin
  SQL := 'DELETE FROM Aluno WHERE Nome_Aluno = ' +
  IntToStr (Aluno.Id_Aluno);
  Result := ExecutarComando(SQL) > 0;
end;



function TAlunoDAO.InserirAluno(pAluno: TAluno): Boolean;
var
  SQL: string;
begin
  SQL := 'INSERT INTO Aluno VALUES ( Default, '
  + QuotedStr(pAluno.nomeCompleto) +
    ', ' + QuotedStr(FormatDateTime('yyyy-mm-dd', pAluno.DataNasc)) + ', ' +
    QuotedStr(pAluno.senha) + ')';
  Result := ExecutarComando(SQL) > 0;
end;



procedure TAlunoDAO.ListaAluno(Ds: TFDQuery);
var
  i: integer;
begin
  i := 0;
  FListarAluno.Clear;



  while not Ds.Eof do
  begin
    FListarAluno.Add(TAluno.Create);
    FListarAluno[i].Id_Aluno:= Ds.FieldByName('id_Aluno').AsInteger;
    FListarAluno[i].nomeCompleto:= Ds.FieldByName('nome_completo').AsString;
    FListarAluno[i].DataNasc:= Ds.FieldByName('data_nascimento').AsDateTime;
    FListarAluno[i].Responsavel:= Ds.FieldByName('nome_Responsavel').AsString;
    FListarAluno[i].Turma:= Ds.FieldByName('Serie').AsString;
    FListarAluno[i].Turno:= Ds.FieldByName('Turno').AsString;
    FListarAluno[i].senha := Ds.FieldByName('senha').AsString;
    Ds.Next;
    i := i + 1;
  end;
end;



function TAlunoDAO.RecuperaNome(user: string): String;
var
  SQL: String;
  Retorno: TFDQuery;
begin
  SQL := 'SELECT * FROM Aluno WHERE Nome_Completo = ' + QuotedStr(user);
  if HaRegistro(SQL) > 0 then
    Retorno := RetornarDataSet(SQL);
  Result := Retorno.FieldByName('id_Aluno').AsString;
end;



function TAlunoDAO.VerificarLogin(nome, senha: string): Boolean;
var
  SQL: string;
begin
  SQL := 'SELECT * from Aluno WHERE Nome_Completo = ' + QuotedStr(Nome) +
    ' and senha = ' + QuotedStr(Senha);



  Result := HaRegistro(SQL) > 0;
end;
end.







uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants,
   System.Classes, Vcl.Graphics,   Vcl.Menus,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Vcl.ComCtrls,
  Vcl.Imaging.pngimage, Vcl.ExtCtrls, uAluno, uProfessor,
  uPgInicialProf, uAlunoDAO, uProfessorDAO, System.UITypes,
    System.Generics.Collections, Data.DB;



type
  TFrmCadastroAluno = class(TForm)
    Image1: TImage;
    EdtNomeAluno: TEdit;
    EdtNomeResponsavel: TEdit;
    EdtSenha: TEdit;
    EdtConfirmaSenha: TEdit;
    DateAluno: TDateTimePicker;
    CBSerie: TComboBox;
    Label1: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    Label4: TLabel;
    Label5: TLabel;
    Label6: TLabel;
    BtCadastroAluno: TButton;
    Label7: TLabel;
    CBTurno: TComboBox;
    Procedure CapturarEdit;
    Procedure LimparEdit;
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure PreencherListView(pListaAluno: TList <TAluno>);
    procedure CarregarColecao;
    procedure BtCadastroAlunoClick(Sender: TObject);



  private

  public
    { Public declarations }
  end;

var
  FrmCadastroAluno: TFrmCadastroAluno;
  Aluno: TAluno;
  AlunoDAO: TAlunoDAO;
  LVListaAluno: TListView;


implementation



{$R *.dfm}


{ TFrmCadastroAluno }





procedure TFrmCadastroAluno.BtCadastroAlunoClick(Sender: TObject);
begin
  CapturarEdit;
  if AlunoDAO.InserirAluno(Aluno) = true then
    MessageDlg('Aluno Cadastrado com Sucesso!', mtConfirmation, [mbOK], 1);
  LimparEdit;
  //PListarAluno.Show;   Não tá funcionando
  FrmCadastroAluno.Hide;
  CarregarColecao;
end;


procedure TFrmCadastroAluno.CapturarEdit;
begin
  Aluno.nomeCompleto:= edtNomeAluno.Text;
  Aluno.DataNasc := DateAluno.DateTime;
  Aluno.Responsavel := EdtNomeResponsavel.Text;
  Aluno.Senha := edtSenha.Text;
  Aluno.Senha := edtConfirmaSenha.Text;
  Aluno.Turma := CBSerie.Text;
  Aluno.Turno := CBTurno.Text;
end;



procedure TFrmCadastroAluno.CarregarColecao;
begin
try
    PreencherListView(AlunoDAO.BuscaAluno);
  except
    on e: exception do
      raise exception.Create(e.Message);
  end;
end;



procedure TFrmCadastroAluno.FormCreate(Sender: TObject);
begin
     DateAluno.DateTime := Now;
     AlunoDAO := TAlunoDAO.Create;
     Aluno := TAluno.Create;
end;



procedure TFrmCadastroAluno.FormDestroy(Sender: TObject);
begin
  try
  if Assigned(Aluno) then
    FreeAndNil(Aluno);
  if Assigned(AlunoDAO) then
    FreeAndNil(AlunoDAO);
  except
    on e: exception do
      raise exception.Create(e.Message);
  End;
  end;

procedure TFrmCadastroAluno.LimparEdit;
begin
  edtNomeAluno.clear;
  edtNomeResponsavel.clear;
  CBTurno.clear;
  CBSerie.clear;
end;

procedure TFrmCadastroAluno.PreencherListView(pListaAluno: TList <TAluno>);

var
  I: integer;
  tempItems: TListItem;
begin
  if Assigned(pListaAluno) then
  begin
    LVListaAluno.clear;



    for I := 0 to pListaAluno.Count - 1 do
    begin
      tempItems := LVListaAluno.Items.Add;
      tempItems.Caption := (TAluno(pListaAluno[I]).nomeCompleto);
      tempItems.Data := TAluno(pListaAluno[I]);
      tempItems.SubItems.Add(TAluno(pListaAluno[I]).Responsavel);
      tempItems.SubItems.Add(TAluno(pListaAluno[I]).Turma);
      tempItems.SubItems.Add(TAluno(pListaAluno[I]).Turno);
    end
  end
  else
    ShowMessage('Nada Encontrado')
end;

end.






unit uDM;

interface

uses
System.SysUtils, System.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.FB,
FireDAC.Phys.FBDef, FireDAC.Stan.Param, FireDAC.DatS, FireDAC.DApt.Intf,
FireDAC.DApt, FireDAC.VCLUI.Wait, FireDAC.VCLUI.Login, FireDAC.Comp.UI,
Data.DB, FireDAC.Comp.DataSet, FireDAC.Comp.Client, FireDAC.Phys.IBBase,
FireDAC.Phys.PG, FireDAC.Phys.PGDef, FireDAC.ConsoleUI.Wait;


type
TDM = class(TDataModule)
Conn: TFDConnection;
private
{ Private declarations }
public
{ Public declarations }
end;

var
DM: TDM;

implementation

{%CLASSGROUP 'System.Classes.TPersistent'}

{$R *.dfm}

end.


Geovanna Biscaia

Geovanna Biscaia

Responder

Que tal ter acesso a um e-book gratuito que vai te ajudar muito nesse momento decisivo?

Ver ebook

Recomendado pra quem ainda não iniciou o estudos.

Eu quero
Ver ebook

Recomendado para quem está passando por dificuldades nessa etapa inicial

Eu quero

Utilizamos cookies para fornecer uma melhor experiência para nossos usuários, consulte nossa política de privacidade.

Aceitar