Thanks for all those who provided me with their interpretation of how Blob
data
transfer can happen between memory and database BLOB fields. Special thanks
to David, Miles and Grant.
As I suspected it really is quite trivial, its only complex when
Help.examples
leave out data definitions and assume that the reader already knows what
he's
reading the Help files to find out.
The following is what I ended up with, in case it might be of use to someone
else.
(*==========================================================================
====
BLOB STUFF -
I have used a Unit, without a Form, in which to store data and associated
definitions. This unit called "Global" is put in the users statement of most
of the units making up the application that need to share this particular
data.
The variables used in this part of the BLOB business are defined as follows
:
const
Storage = 170;
var
ProgData : ARRAY[0..(Storage * 6)] OF byte; { 1020 bytes
of Prog Data}
Account : string[4]; { The value for the Database index field
('Account') ..
.. is obtained from a data entry box
and in this application...
.. the Database table is
resident on the Main Form. }
The Database Table amongst others, has a numeric field 'Account'
..
...
and a BLOB field 'ProgData' }
----------------------------------------------------------------------------
--*)
procedure TMainForm.DBAcctEditChange(Sender: TObject); { Establish Account
# }
begin
Global.Account := AcctEdit.Text; { The Account key field value, by which
...
... the database is indexed ( as a numeric
value) }
end;
{---------------------------------------------------------------------------
-----------}
function DbaseToProgData : boolean; {Transfers dBase 'ProgData' field data
...
... into Global.ProgData returns TRUE if
the dBase record exists}
var BStream : TBlobStream; { The temporary stream via which data is
transferred}
begin
MainForm.Table1.Active := TRUE; {dBase table (Table1) must be
active ...}
MainForm.Table1.Edit;
{... and opened for editing}
if MainForm.Table1.FindKey([Global.Account]) then {Account record
exists?}
begin
try
BStream := TBlobStream.Create(TBlobField(MainForm.Table1.
FieldByName('ProgData')),bmRead);
// Creates a BlobStream associated with the Database BLOB field and
// reads data out of it into the stream
BStream.Read(Global.ProgData,SizeOf(Global.ProgData));
// Memory, (as the array defined in Unit Global,) reads the data in the
stream
// for the correct length of the data
DBaseToProgData := TRUE; { Return TRUE as Record #(Account)
existed}
finally
BStream.Free; { Always give back the space taken up by the stream,
..
... the stream memory is only used
temporarily}
end;
end
else { dBase record for the Account not
found}
DBaseToProgData := FALSE;
MainForm.Table1.Active := FALSE; { Don't leave the database table
vulnerable}
end;
{---------------------------------------------------------------------------
------------}
function ProgDataTodBase : boolean;
{Puts ProgData into dBase and returns true if dBase.Account ...
... exists. Just the above procdure
backwards}
var BStream : TBlobStream;
begin
MainForm.Table1.Active := TRUE;
MainForm.Table1.Edit;
if MainForm.Table1.FindKey([Account]) then
begin
try
MainForm.Table1.Edit;
BStream := TBlobStream.Create(TBlobField(MainForm.Table1.
FieldByName('ProgData')),bmWrite);
// Write the ProgData (memory array) into the stream
BStream.Write(Global.ProgData,SizeOf(Global.ProgData));
// Then from the stream into the Database (BLOB) field ('ProgData')
ProgDataToDBase := TRUE;
finally
BStream.Free;
end;
end
else
ProgDataToDBase := FALSE;
MainForm.Table1.Active := FALSE;
end;
{---------------------------------------------------------------------------
---}
{ Tests the above two procedures by testing for data transfer and retrieval
of a single byte of data at an arbitary location (123) in the memory array
using
different values in different dbase records}
procedure TMainForm.GetBtnClick(Sender: TObject);{Gets dBase data into
ProgData}
begin
if DBaseToProgData then
MainForm.GetBox.Text := IntToStr(ProgData[123])
else MainForm.GetBox.Color := clRed; { Account doesn't
exist}
end;
procedure TMainForm.PutBtnClick(Sender: TObject); {Puts ProgData into
dBase}
begin
if MainForm.PutBox.Text <> '' then
begin
ProgData[123] := StrToInt(MainForm.PutBox.Text);
if ProgDataToDBase then MainForm.PutBox.Text := IntToStr(ProgData[(123])
else MainForm.PutBox.Color := clRed;
end;
end;
{ Didn't bother puting the colours back to normal if the database record
wasn't found, it's only for testing. Add another button if you want.}
{---------------------------------------------------------------------------
---}
Cheers
Colin.
---------------------------------------------------------------------------
New Zealand Delphi Users group - Delphi List - [EMAIL PROTECTED]
Website: http://www.delphi.org.nz