Hola...
Cita:
Empezado por cestradar
mmmmmmmmmmmmmmm, no, no todos
¿Qué ventaja me da tener esa función en una librería a tener un componente TQuery? , digo, igual voy a configurarlos, ya sea con parámetros o desde código.
¿ahorro de código?, igual voy a definir variables tipo TQuery o TDataSet para recibir esa función, no le veo la utilidad real
|
Pues como bien comentas, ahorro de código...
No es lo mismo hacer:
Código Delphi
[-]
var
AQuery1: TQuery;
AQuery2: TQuery;
begin
AQuery1 := TQuery.Create(nil);
with AQuery1 do
try
SQL.Text := 'select * from tabla1';
Prepare;
Open;
finally
Free
end;
AQuery2 := TQuery.Create(nil);
with AQuery2 do
try
SQL.Text := 'select * from tabla2 where campo1 = 5';
Prepare;
Open;
finally
Free
end;
AQuery1 := TQuery.Create(nil);
with AQuery1 do
try
SQL.Text := 'select * from tabla3 where campo1 = 7 and campo2 = ''Hola''';
Prepare;
Open;
finally
Free
end;
end;
A tener algo como:
Código Delphi
[-]
unit MyFunctions;
interface
uses
Classes, DB;
function CreateQuery(SQLStr: string): TQuery;
implementation;
function CreateQuery(SQLStr: string): TQuery;
begin
Result := TQuery.Create(nil);
with Result do
begin
SQL.Text := SQLStr;
Prepare
end
end;
end.
unit Form1;
interface
implementation
uses MyFunctions;
procedure TForm1.Button1Click(Sender: TObject);
begin
with CreateQuery('select * from tabla1') do
try
Open;
finally
Free
end;
with CreateQuery('select * from tabla2 where campo1 = 5') do
try
Open;
finally
Free
end
end;
end.
Ves la diferencia?
Me ahorré declarar 2 variables y re-escribir bastante código...
Saludos...