一、读一个图片到数据库

var

inimage1:TImage;
TPStream:TMemoryStream;

 

inimage1 := TImage.Create(nil);
//读取图片
TPStream := TMemoryStream.Create;
Image1.Picture.SaveToStreamWithFileExt(TPStream, 'jpg');//生成jpeg图片
inimage1.Free;
TPStream.Position := 0;
sqlst := format('UPDATE userinfo SET tp=:tp WHERE id=%d',[userid]);
Form1.SQLQuery1.Close;
Form1.SQLQuery1.SQL.Text := sqlst;
Form1.SQLQuery1.ParamByName('tp').SetBlobData(TPStream.Memory,TPStream.Size); 
Form1.SQLQuery1.ExecSQL;
TPStream.Free;

二、从数据库中得到图片数据

TPStream:=TMemoryStream.Create;
TPStream.Write(Form1.SQLQuery1.FieldByName('tp').AsBytes,Length(Form1.SQLQuery1.FieldByName('tp').AsBytes));
TPStream.Position:=0;
Image1.Picture.LoadFromStream(TPStream);
TPStream.Free;

三、写一个浮点数据到数据库

var

psin:PSingle;

fcLen:integer;

 

 psin:= GetMem(fcLen * SizeOf(single));

//写浮点数据值

sqlst := format(
'UPDATE userinfo SET tzm=:tzm WHERE id=%d',
[userid]);
Form1.SQLQuery1.Close;
Form1.SQLQuery1.SQL.Text := sqlst;
Form1.SQLQuery1.ParamByName('tzm').SetBlobData(psin, fcLen * SizeOf(single));
Form1.SQLQuery1.ExecSQL;
FreeMemAndNil(psin);

四、从数据库中读取浮点数

TZMStream:=TMemoryStream.Create;
lenp:=Length(Form1.SQLQuery1.FieldByName('tzm').AsBytes);
psin:=GetMem(lenp);
Move(Form1.SQLQuery1.FieldByName('tzm').AsBytes[0],psin^,lenp); 
FreeMemAndNil(psin);

posted on 2024-09-27 19:28  禁卫军  阅读(14)  评论(0编辑  收藏  举报