Comment puis-je exécuter une commande SQL avec un paramètre blob dans dbx?

StackOverflow https://stackoverflow.com/questions/325874

  •  11-07-2019
  •  | 
  •  

Question

J'ai un TSqlDataSet qui a un champ blob. Je dois obtenir les données de ce champ blob dans l'événement BeforeUpdateRecord du fournisseur et exécuter une commande de mise à jour. J'ai essayé ceci:

Cmd := TSQLQuery.Create(nil);
try
  Cmd.SQLConnection := SQLConnection;
  Cmd.CommandText := 'UPDATE MYTABLE SET IMAGE = :PIMAGE WHERE ID = :PID';
  Cmd.Params.CreateParam(ftBlob, 'PIMAGE ', ptInput).Value := DeltaDS.FieldByName('IMAGE').NewValue; //blob field
  Cmd.Params.CreateParam(ftString, 'PID', ptInput).Value := DeltaDS.FieldByName('ID').NewValue;
  Cmd.ExecSQL;
finally
  Cmd.Free;
end;

Lorsque j'exécute, j'obtiens une erreur EDatabaseError avec le message: "Aucune valeur pour le paramètre PIMAGE.

Qu'est-ce qui me manque?

Était-ce utile?

La solution

Répondant à ma propre question, la bonne façon de le faire est la suivante:

const
  SQL = 'UPDATE MYTABLE SET IMAGE = :PIMAGE WHERE ID = :PID;';
var
  Params: TParams;
begin
  Params := TParams.Create(nil);
  try
    Params.CreateParam(ftBlob, 'PIMAGE', ptInput).AsBlob := DeltaDS.FieldByName('IMAGE').NewValue;
    Params.CreateParam(ftString, 'PID', ptInput).Value := DeltaDS.FieldByName('ID').NewValue;
    SQLConnection.Execute(SQL, Params);
  finally
    Params.Free;
  end;
end;

Autres conseils

Avez-vous essayé de tester avec un autre pilote (par exemple, ODBC)? Il est possible que l'erreur ne soit pas dans votre code. Cette approche (changer de fournisseur de données / de pilote) m’a aidé à résoudre certains problèmes déroutants qui ne se sont pas révélés miens.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top