Normally, I would suggest using AssignFieldValue, but I've encountered
several problems with that function alone.
The problem with Fields and Parameters is that they sometimes back
different assumptions about how to treat a datatype. ftBlob is one of
those datatypes. We've encountered several problems with Null values and
some access violations when copying Blob fields to Blob parameters. It's
been particularly troublesome when the source query (with the TFields) is
of a different database than the destination Insert query (with the
TParams).
Here's a nice function that handles it. It will copy the value of a field
of any type (that *I've* dealt with) into a parameter. Note that it makes
use of an enumerated type etDatabaseKind - this is one of our own
conjurations that tells the function what kind of database engine the
destination parameter is going to.
---- code snippet -----
etDatabaseKind = (dbkUnknown, dbkInterbase, dbkOracle, dbkAccess,
dbkSQLServer);
{ Copy the value from a field into a parameter, with special handling for
Blob fields, Oracle and NULL values. Things that must be accomplished
here are: Copy the value correctly, Set the datatype, Set Bound:=True.}
procedure CopyFieldToParam(aField: TField; aParam: TParam;
ParamDBKind: etDatabaseKind);
begin
with aParam do
begin
if (aField.DataType = ftBlob) then
begin
DataType := ftBlob;
{ The second condition of the following if statement assumes that a
Blob field is NULL if AsString=''. This is necessary for
situations
where aField is an Interbase field and aParam is an Oracle
parameter, which occasionally causes a system hang. Can't trust
VarIsNull(aField.Value) either. }
if (TBlobField(aField).IsNull) or
(TBlobField(aField).AsString = '') then
begin
{ Without the following two lines, an access violation occurs
occasionally under IB. Can't use AsBlob:='' for the same reason.
However, can't do this under Oracle though because you can't
change the
Param.DataType on the fly. Can't use Value:='' for the same
reason.}
if ParamDBKind <> dbkOracle then
AsString := '';
Clear;
end
else
AssignFieldValue(aField, aField.AsVariant);
end
else
AssignFieldValue(aField, aField.AsVariant);
Bound := True;
end;
end;
--- end code snippet ---
Cheers.
BJ...
----------
From: Mark Derricut[SMTP:[EMAIL PROTECTED]]
Reply To: [EMAIL PROTECTED]
Sent: Thursday, 11 February 1999 14:00
To: Multiple recipients of list delphi
Subject: RE: [DUG]: Inserting Blobs Via SQL
On Thu, 11 Feb 1999, BJ Wilson wrote:
> This then requires that you have your Blob field in memory so that you
> can load ParamByName('Layout').AsBlob.
I dont actually have my Blob in memory, but in the current dataset of
another TQuery, however, TField doesn't have an .AsBlob property, I see
that TBlobData is defined as String so I might just try using .AsString
for both of them and see how that works :P
Mark
NP: AngelDust - Conjourings from World of Illusion
---------------------------------------------------------------------------
New Zealand Delphi Users group - Delphi List - [EMAIL PROTECTED]
Website: http://www.delphi.org.nz
---------------------------------------------------------------------------
New Zealand Delphi Users group - Delphi List - [EMAIL PROTECTED]
Website: http://www.delphi.org.nz