Does anyone know how I can write and read to a binary (or blob) field using
Delphi and ADO Express ?
I've looked for SaveToFile and LoadFromFile, but no such luck.
Any help would be greatly appreciated.
Thanks.
Delphi 5 (Update Pack 2)
ADOExpress (Update Pack 2)
Database MSSQL 7.0 (SP2)
--
----------------------------------------------------------------------------
-
Brian Driscoll, MCSD
Technicien senior systèmes ordinés
brian.d...@groupecadec.com
Groupe Cadec
Bromptonville, Québec, Canada
http://www.groupecadec.com
----------------------------------------------------------------------------
-
{---------------------------------------------------------------------------
-------}
procedure TfrmMain.SaveTest();
var
sFileName : string;
BS: TADOBlobStream;
begin
dmData.qryVersion.Open;
dialOpen.Execute;
sFileName := dialOpen.FileName;
ShowMessage(sFileName);
// Place in edit mode.
if not (dmData.qryVersion.State in [dsEdit, dsInsert]) then
dmData.qryVersion.Edit;
// Create blob stream.
BS :=
TADOBlobStream.Create(TBlobField(dmData.qryVersion.FieldByName('binaryExec')
), bmReadWrite);
try
BS.LoadFromFile(sFileName);
dmData.qryVersion.Post;
finally
BS.Free;
end;
dmData.qryVersion.Close;
end;
{---------------------------------------------------------------------------
-------}
Brian Driscoll <brian.d...@groupecadec.com> wrote in message
news:8iah3p$i7...@bornews.borland.com...
Not too sure why that code wouldn't work. Technically the blobstream should be
freed before the Post, so that may be the problem. TBlobField also has a
LoadFromFile method which you can call
directly:
TBlobField(dmData.qryVersion.FieldByName('binaryExec') ).LoadFromFile(sFileName)
;
Give that a shot.
Mark
Tried the code...
>
TBlobField(dmData.qryVersion.FieldByName('binaryExec') ).LoadFromFile(sFileN
ame)
but I'm getting an 'invalid class typecast'. Any idea why.
BTW the MSSQL field in question is defined as a varbinary.
Thanks again.
Brian Driscoll
Mark Edington (Borland) <medi...@nolunchmeat.com> wrote in message
news:39494331@dnews...
> but I'm getting an 'invalid class typecast'. Any idea why.
> BTW the MSSQL field in question is defined as a varbinary.
Ok, I think we are getting somewhere now. If the field is a VarBinary then it
won't be mapped to a TBlobField and therefore the code I gave you is invalid.
For a VarBinary field you'll need a helper function to read the file into the
field:
Try this:
function StringFromFile(const FileName: string): string;
begin
with TFileStream.create(FileName, fmOpenRead) do
try
SetLength(Result, Size);
Read(Pointer(Result)^, Size);
finally
Free;
end;
end;
dmData.qryVersion.FieldByName('binaryExec')
.SetAsString(StringFileFile(AFileName));
That ought to do it.
Mark
I ended up changing the MSSQL field to an Image type (which is probably
more
appropriate for the type of data I'm using). Now the LoadFromFile and
SaveToFile work fine.
Thanks for putting me on the right track.
BTW, there is no SetAsString with a TVarBinaryField, only for the
TIBStringField (which is InterBase I think).
----- Original Message -----
From: Mark Edington (Borland) <medi...@nolunchmeat.com>
Newsgroups: borland.public.delphi.database.ado
Sent: Friday, June 16, 2000 2:34 PM
Subject: Re: Blob fields and ADO ?
> I ended up changing the MSSQL field to an Image type (which is probably
> more appropriate for the type of data I'm using). Now the LoadFromFile and
> SaveToFile work fine.
Sounds good.
> BTW, there is no SetAsString with a TVarBinaryField, only for the
> TIBStringField (which is InterBase I think).
Whoops. That should've read:
dmData.qryVersion.FieldByName('binaryExec').AsString :=
StringFileFile(AFileName);
Sorry about the confusion.
Mark