unit UCustomerList;
interface
uses
classes, system.Types, system.SysUtils, ZAbstractRODataset, ZAbstractDataset,
ZDataset, ZAbstractConnection, ZConnection ,contnrs;
type
TCustomer=class
private
FKDNR : Integer;
FName:
string;
FVorname:
string;
FFirma :
string;
FProdukt:
string;
FAnzahl : Integer;
FPreis : Currency;
procedure SetKDNR(
const Value: Integer);
procedure SetName(
const Value:
string);
procedure SetVorname(
const Value:
string);
procedure SetFirma (
const Value :
string);
procedure SetProdukt(
const Value :
String);
procedure SetAnzahl (
const Value : Integer);
procedure SetPreis(
const Value: Currency);
published
property KDNR: integer
read FKDNr
write SetKDNR;
property Name:
string read FName
write SetName;
property Vorname:
string read FVorname
write SetVorname;
property Firma:
string read FFirma
write SetFirma;
property Produkt:
string read FProdukt
write setProdukt;
property Anzahl: Integer
read FAnzahl
write SetAnzahl;
property Preis: Currency
read FPreis
write SetPreis;
end;
TCustomerList=class(TObjectList)
function getItem(
index: Integer): TCustomer;
virtual;
procedure setItem(
index: Integer; Customer: TCustomer);
virtual;
procedure LoadFromDB(con: TZConnection);
procedure SavetoDB(con: TZConnection);
public
property Items[
index: Integer]: TCustomer
read getItem
write setItem;
default;
procedure Insert(
index: Integer; Customer: TCustomer);
virtual;
function Add(Customer: TCustomer): Integer;
virtual;
function Remove(Customer: TCustomer): Integer;
virtual;
function IndexOf(Customer: TCustomer): Integer;
virtual;
function First: TCustomer;
virtual;
function Last: TCustomer;
virtual;
function AddCustomer(KDNR: integer;
Name:
string; Vorname:
string; Firma:
string; Produkt:
string; Anzahl: Integer; Preis: Currency):integer;
end;
implementation
{ TCustomer }
procedure TCustomer.SetAnzahl(
const Value: Integer);
begin
FAnzahl := Value;
end;
procedure TCustomer.SetFirma(
const Value:
string);
begin
FFirma := Value;
end;
procedure TCustomer.SetKDNR(
const Value: Integer);
begin
FKDNR:= Value;
end;
procedure TCustomer.SetName(
const Value:
string);
begin
FName := Value;
end;
procedure TCustomer.SetPreis(
const Value: Currency);
begin
FPreis := Value;
end;
procedure TCustomer.SetProdukt(
const Value:
String);
begin
FProdukt := Value;
end;
procedure TCustomer.SetVorname(
const Value:
string);
begin
FVorname := Value;
end;
{ TCustomerList }
function TCustomerList.Add(Customer: TCustomer): Integer;
begin
Result:=inherited Add(Customer);
end;
procedure TCustomerList.setItem(
index: Integer; Customer: TCustomer);
begin
inherited Items[
index]:=Customer;
end;
function TCustomerList.AddCustomer(KDNR: integer;
Name, Vorname, Firma,
Produkt:
string; Anzahl: Integer; Preis: Currency): integer;
var
Customer: TCustomer;
begin
Customer:=TCustomer.Create;
Customer.KDNR:=KDNR;
Customer.
Name:=Name;
Customer.Vorname:=Vorname;
Customer.Firma:=Firma;
Customer.Produkt:=Produkt;
Customer.Anzahl:=Anzahl;
Customer.Preis:=Preis;
self.Add(Customer);
end;
function TCustomerList.Remove(Customer: TCustomer): Integer;
begin
Result:=inherited Remove(Customer);
end;
function TCustomerList.First: TCustomer;
begin
Result:=TCustomer(
inherited First());
end;
function TCustomerList.getItem(
index: Integer): TCustomer;
begin
Result:=TCustomer(
inherited Items[
index]);
end;
function TCustomerList.IndexOf(Customer: TCustomer): Integer;
begin
Result:=inherited IndexOf(Customer);
end;
procedure TCustomerList.Insert(
index: Integer; Customer: TCustomer);
begin
inherited Insert(
index, Customer);
end;
function TCustomerList.Last: TCustomer;
begin
Result:=TCustomer(
inherited Last());
end;
procedure TCustomerList.LoadFromDB(con: TZConnection);
var
zqyMain: TZQuery;
Customer: TCustomer;
begin
zqyMain:=TZQuery.Create(
nil);
Try
self.Clear;
//Dank TObjectlist werden auch alle bereits vorhandenen Objecte automatisch freigegeben
zqyMain.connection:=con;
zqyMain.sql.text:='
SELECT * FROM WARENVERKAUF1';
zqyMain.active:=True;
while not zqyMain.eof
do
begin
Customer:=TCustomer.Create;
Customer.KDNR:=zqyMain.fieldbyname('
KDNR').AsInteger;
Customer.
Name:=zqyMain.fieldbyname('
Name').AsString;
Customer.Vorname:=zqyMain.fieldbyname('
Vorname').AsString;
Customer.Firma:=zqyMain.fieldByName('
Firma').AsString;
Customer.Produkt:=zqyMain.fieldbyname('
Produkt').AsString;
Customer.Anzahl:=zqyMain.FieldByName('
Anzahl').AsInteger;
Customer.Preis:=zqyMain.fieldbyname('
Preis').AsCurrency;
self.Add(Customer);
end;
zqyMain.active:=False;
Finally
zqyMain.free;
End;
end;
procedure TCustomerList.SavetoDB(con: TZConnection);
var
zqryMain: TZQuery;
i: Integer;
begin
zqryMain:=TZQuery.Create(
nil);
try
zqryMain.connection:=con;
zqryMain.sql.text:='
INSERT INTO WARENVERKAUF1 (KDNR, NAME, VORNAME, FIRMA, PRODUKT, ANZAHL, PREIS) VALUES (:KNR, :NAM, :VNA, :FIR, :PRO, :ANZ, :PRE)';
zqryMain.params.parseSQL(zqryMain.sql.text, True);
for i:=0
to self.count-1
do
begin
zqryMain.params.ParamValues['
KNR']:=self[i].KDNR;
zqryMain.params.paramValues['
NAM']:=self[i].
Name;
zqryMain.params.paramValues['
VNA']:=self[i].Vorname;
zqryMain.params.ParamValues['
FIR']:=self[i].Firma;
zqryMain.Params.ParamValues['
PRO']:=self[i].Produkt;
zqryMain.Params.ParamValues['
ANZ']:=self[i].Anzahl;
zqryMain.params.paramValues['
PRE']:=self[i].Preis;
zqryMain.ExecSQL;
end;
finally
zqryMain.free;
end;
end;
end.