Ver Mensaje Individual
  #3  
Antiguo 04-07-2017
Avatar de olbeup
olbeup olbeup is offline
Miembro
 
Registrado: jul 2005
Ubicación: Santiago de la Ribera (España)
Posts: 685
Reputación: 19
olbeup Va camino a la fama
Hola webmasterplc,

En principio tienes que entender un poco sobre OOP, de lo contrario te vas a perder, a ver si con esto te puedo ayudar un poco a entender como funciona y vayas aprendiendo tú poco a poco.

Código Delphi [-]
unit FyPTMyEquipos;

interface

type

  { TEquipo }

  TEquipo = class(TObject)
  private
    FIDCodigo: Integer;
    FCodigo: String;
    FModelo: String;
    FDescripcion: String;
    procedure SetIDValue(IDValue: Integer);
    function GetExisteCodigo: Boolean;
  protected
  public
    constructor Create;
    destructor Destroy; override;
    function Post: Integer;
  published
    property IDCodigo: Integer read FIDCodigo write SetIDValue;
    property Codigo: String read FCodigo write FCodigo;
    property Modelo: String read FModelo write FModelo;
    property Descripcion: String read FDescripcion write FDescripcion;
    property ExisteCodigo: Boolean read GetExisteCodigo;
  end;

implementation

uses
  ADODB;

  { TEquipo }

constructor TEquipo.Create;
begin
  inherited;
  FIDCodigo    := -1;
  FCodigo      := '';
  FModelo      := '';
  FDescripcion := '';
end;

destructor TEquipo.Destroy;
begin
  inherited;
end;

function TEquipo.Post: Integer;;
var
  FadoCnn: TADOConnection;
begin
  case FIDCodigo of
    -1..0: // Añadir un regristro nuevo
      begin
        FadoCnn := TADOConnection.Create(nil);
          FadoCnn.LoginPrompt := False;
          FadoCnn.ConnectionString := "Tus datos de conexión";

        with TADOQuery.Create(nil) do
        begin
          Connection := FadoCnn;

          SQL.Add('INSERT INTO csequipos');
          SQL.Add('  (');
          SQL.Add('    eq_codigo');
          SQL.Add('    ,eq_modelo');
          SQL.Add('    ,eq_descripcion');
          SQL.Add('   )');
          SQL.Add('   VALUES');
          SQL.Add('     (');
          SQL.Add(        QuotedStr(Codigo));
          SQL.Add('       ,' + QuotedStr(Modelo));
          SQL.Add('       ,' + QuotedStr(Descripcion));
          SQL.Add('     )');

          SQL.Add('SELECT @@IDENTITY AS FIELDINTEGER');

          Open;

          FIDCodigo := FieldByName('FIELDINTEGER').AsInteger;
          Result := FIDCodigo;

          FreeAndNil(FadoCnn);
          Free;
        end;
      end;
    else begin // Actualizar registro
      Result := FIDCodigo;

      FadoCnn := TADOConnection.Create(nil);
        FadoCnn.LoginPrompt := False;
        FadoCnn.ConnectionString := "Tus datos de conexión";

      with TADOQuery.Create(nil) do
      begin
        Connection := FadoCnn;

        SQL.Add('UPDATE csequipos');
        SQL.Add('  SET');
        SQL.Add('    eq_codigo       = ' + QuotedStr(Codigo));
        SQL.Add('    ,eq_modelo      = ' + QuotedStr(Modelo));
        SQL.Add('    ,eq_descripcion = ' + QuotedStr(Descripcion));
        SQL.Add('  WHERE ideq_codigo = ' + IntToStr(FIDCodigo));

        ExecSQL;

        FreeAndNil(FadoCnn);
        Free;
      end;
    end;
  end;
end;

procedure TEquipo.SetIDValue(IDValue: Integer);
var
  FadoCnn: TADOConnection;
begin
  FadoCnn := TADOConnection.Create(nil);
    FadoCnn.LoginPrompt := False;
    FadoCnn.ConnectionString := "Tus datos de conexión";

  with TADOQuery.Create(nil) do
  begin
    Connection := FadoCnn;

    SQL.Add('SELECT');
    SQL.Add('    IDCODIGO');
    SQL.Add('    ,eq_codigo');
    SQL.Add('    ,eq_modelo');
    SQL.Add('    ,eq_descripcion');
    SQL.Add('  FROM csequipos');
    SQL.Add('  WHERE IDCODIGO = ' + IntToStr(IDValue));

    Prepared := True;

    Open;

    FIDCodigo    := FieldByName('IDCODIGO').AsInteger;
    FCodigo      := FieldByName('eq_codigo').AsString;
    FModelo      := FieldByName('eq_modelo').AsString;
    FDescripcion := FieldByName('eq_descripcion').AsString;

    FreeAndNil(FadoCnn);
    Free;
  end;
end;

function TEquipo.ExisteCodigo: Boolean;
var
  FadoCnn: TADOConnection;
begin
  FadoCnn := TADOConnection.Create(nil);
    FadoCnn.LoginPrompt := False;
    FadoCnn.ConnectionString := "Tus datos de conexión";

  with TADOQuery.Create(nil) do
  begin
    Connection := FadoCnn;

    SQL.Add('SELECT');
    SQL.Add('    IDCODIGO AS FIELDINTEGER');
    SQL.Add('  FROM csequipos');
    SQL.Add('  WHERE eq_codigo = ' + QuotedStr(FCodigo));

    Prepared := True;

    Open;

    if (FieldByName('FIELDINTEGER').AsInteger = 0) then
      Result := False
    else
      Result := (FIDCodigo <> FieldByName('FIELDINTEGER').AsInteger);

    FreeAndNil(FadoCnn);
    Free;
  end;
end;

y en un formulario nuevo y en la parte privada pones:
Código Delphi [-]
  ...
  private
    MiEquipo: TEquipo;

y cuando se destruye el formulario:
Código Delphi [-]
procedure TForm1.FromDestroy(Sender: TObject);
begin
  FreeAndNil(MiEquipo);
end;

En los TEdit:
Código Delphi [-]
procedure TForm1.Edit1Change(Sender: TObject);
begin
  MiEquipo.Codigo := Edit1.Text;
end;

procedure TForm1.Edit2Change(Sender: TObject);
begin
  MiEquipo.Modelo := Edit2.Text;
end;

procedure TForm1.Edit3Change(Sender: TObject);
begin
  MiEquipo.Descripcion := Edit3.Text;
end;

En un Boton donde ponga Añadir
Código Delphi [-]
procedure TForm1.NuevoClick(Sender: TObject);
begin
  MiEquipo := TEquipo.Create;

  Edit1.Text := '';
  Edit2.Text := '';
  Edit3.Text := '';
end;

En un Boton donde ponga Modificar
Código Delphi [-]
procedure TForm1.ModificarClick(Sender: TObject);
begin
  MiEquipo := TEquipo.Create;

  with dbgGrid.DataSource.Dataset do
    MiEquipo.IDCodigo := FieldByName('IDCODIGO').AsInteger;

  Edit1.Text := MiEquipo.Codigo;
  Edit2.Text := MiEquipo.Modelo;
  Edit3.Text := MiEquipo.Descripcio;
end;

En un Boton donde ponga Grabar
Código Delphi [-]
procedure TForm1.GrabarClick(Sender: TObject);
begin
  if MiEquipo.ExisteCodigo then
    ShowMessage('Este código ya existe en la DB')
  else
    MiEquipo.Post;
end;

No lo he probado que funcione lo he realizado todo de memoria

Un saludo

p.d.: añade los campos que te faltan,

p.d2. el IDCODIGO y el FIDCodigo lo he puesto yo para identificar cada registro único e inequívoco
__________________
Al hacer una consulta SQL, haz que los demás te entiendan y disfruten de ella, será tú reflejo de tú saber.

Última edición por olbeup fecha: 04-07-2017 a las 09:58:50.
Responder Con Cita