procedure TForm1.WriteBitmapToDatabase(BMP : TBitmap);
var
FRecordID : Integer;
V : Variant;
P : Pointer;
ADOConnection : Connection;
ADORecordset : Recordset;
SQL :
String;
MemoryStream : TMemoryStream;
begin
FRecordID:=AddNewRecord;
coboID.Items.Add(IntToStr(FRecordID));
MemoryStream:=TMemoryStream.Create;
SQL:=Format(sqlWritePicture,[FRecordID]);
ADORecordset:=CoRecordset.Create;
Try
Bmp.SaveToStream(MemoryStream);
MemoryStream.Seek(0,soFromBeginning);
V:=VarArrayCreate([0,MemoryStream.Size], varByte);
P:=VarArrayLock(V);
MemoryStream.
Read(P^,MemoryStream.Size);
ADORecordset.Open(
SQL,FADOConnection,adUseServer,adLockOptimistic,adOptionUnspecified);
ADORecordset.Fields.Item['
TYPE_'].Value:='
BMP';
ADORecordset.Fields.Item['
PICTURE'].Value:=V;
ADORecordset.Update(EmptyParam,EmptyParam);
Finally
VarArrayUnLock(V);
MemoryStream.Free;
ADORecordset:=Nil;
end;
end;
procedure TForm1.btnReadClick(Sender: TObject);
var
FRecordID : Integer;
V : Variant;
P : Pointer;
ADORecordset : Recordset;
SQL :
String;
MemoryStream : TMemoryStream;
begin
Screen.Cursor:=crHourGlass;
MemoryStream:=TMemoryStream.Create;
SQL:=Format(sqlReadPicture,[coboID.Items[coboID.ItemIndex]]);
ADORecordset:=CoRecordset.Create;
Try
ADORecordset.Open(
SQL,FADOConnection,adUseServer,adLockOptimistic,adOptionUnspecified);
V:=VarArrayCreate([0,ADORecordset.Fields.Item['
PICTURE'].ActualSize], varByte);
V:=ADORecordset.Fields.Item['
PICTURE'].Value;
P:=VarArrayLock(V);
MemoryStream.
Write(P^,ADORecordset.Fields.Item['
PICTURE'].ActualSize);
MemoryStream.Seek(0,soFromBeginning);
Image2.Picture.Bitmap.LoadFromStream(MemoryStream);
Finally
VarArrayUnLock(V);
MemoryStream.Free;
ADORecordset:=Nil;
Screen.Cursor:=crDefault;
end;
end;