Thema: Delphi Von ADOQuery zu ADSQuery

Einzelnen Beitrag anzeigen

shmia

Registriert seit: 2. Mär 2004
5.508 Beiträge
 
Delphi 5 Professional
 
#5

Re: Von ADOQuery zu ADSQuery

  Alt 26. Mai 2004, 17:35
Zitat von Kevin:
Code:
// folgendes Beispiel geht einem 3-Spaltigen Stringgrid aus

var aQry:TADOQuery;
     y:Integer;
    sQry:String;
...
aQry:=TADOQuery.Create(nil);
try
  aQry.Connection:=ADOConnection1;
  sQry:='INSERT INTO tabelle (Feld1, Feld2, Feld3) VALUES(:v1, :v2, :v3)';
  for y:=1 to StringGrid1.RowCount do
  begin
    aQry.SQL.Text:=sQry;
    with aQry.Parameters do
    begin
      ParamValues['v1']:=StringGrid1.Cells[0, y];
      ParamValues['v2']:=StringGrid1.Cells[1, y];
      ParamValues['v3']:=StringGrid1.Cells[2, y];
    end; // with
    aQry.ExecSQL;
  end; // for y
finally
  aQry.Free;
end; // try
Du weisst die Eigenschaft SQL.Text innerhalb der Schleife zu. Das ist bestimmt nicht günstig ( )
für die Parameter-Objekte, da diese jedesmal aus der SQL-Anweisung geparst werden.
Deshalb:
Delphi-Quellcode:
aQry:=TADOQuery.Create(nil);
try
  aQry.Connection:=ADOConnection1;
  aQry.SQL.Text:='INSERT INTO tabelle (Feld1, Feld2, Feld3) VALUES(:v1, :v2, :v3)';
  for y:=1 to StringGrid1.RowCount do
  begin
Manchmal ist ADO nicht in der Lage, die Datentypen der Parameter korrekt zu ermitteln.
(dies hängt vom OLE-DB Provider ab; auch die VCL hat hier möglicherweise noch Bugs)
Dann muss man nachhelfen:
Delphi-Quellcode:
aQry.SQL.Text:='INSERT INTO tabelle (Feld1, Feld2, Feld3) VALUES(:v1, :v2, :v3)';
  aQry.Parameters.ParamByName('v1').Datatype := dtInteger;
  ...
  for y:=1 to StringGrid1.RowCount do
Andreas
  Mit Zitat antworten Zitat