>데이터 베이스 >MySQL 튜토리얼 >用ADO控件存取SQL Server数据库图片

用ADO控件存取SQL Server数据库图片

WBOY
WBOY원래의
2016-06-07 15:12:511252검색

用ADO控件 存取 SQL Server 数据库 图片 ,查看资料发现基本都是针对BMP文件进行操作(巨增 数据库 大小),但发现了★eagletian★高手翻译的ADO英文技术文档,里面涉及到了 数据库 存取 JPEG文件的关键技术,在win98+sql server桌面版中测试通过,运行良好,

用ADO控件存取SQL Server数据库图片,查看资料发现基本都是针对BMP文件进行操作(巨增数据库大小),但发现了★eagletian★高手翻译的ADO英文技术文档,里面涉及到了数据库存取JPEG文件的关键技术,在win98+sql server桌面版中测试通过,运行良好,现于源码公开如下:
unit Unit1;

interface

uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
ExtCtrls, DBCtrls, Grids, DBGrids, Db, ADODB,jpeg, StdCtrls,dbtables;
 {一定要USES JPEG单元,使能存储JPG文件格式}
type
TForm1 = class(TForm)
  DataSource1: TDataSource;
  ADOQuery1: TADOQuery;
  DBGrid1: TDBGrid;
  DBNavigator1: TDBNavigator;
  Image1: TImage;
  savebutton: TButton;
  showbutton: TButton;
  OpenDialog1: TOpenDialog;
  ADOQuery1id: TIntegerField;
  ADOQuery1pic: TBlobField;
  procedure savebuttonClick(Sender: TObject);
  procedure showbuttonClick(Sender: TObject);
  procedure DBNavigator1Click(Sender: TObject; Button: TNavigateBtn);
private
  { Private declarations }
public
  { Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.DFM}

function JpegStartsInBlob(PicField:TBlobField):integer;
 var
  ghy: TADOBlobstream;
  buffer:Word;
  hx: string;
 begin
  Result := -1;
  ghy := TADOBlobstream.Create(PicField, bmRead);
  try
  while (Result = -1) and (ghy.Position + 1   begin
   ghy.ReadBuffer(buffer, 1);
   hx:=IntToHex(buffer, 2);
   if hx = ’FF’ then begin
   ghy.ReadBuffer(buffer, 1);
   hx:=IntToHex(buffer, 2);
   if hx = ’D8’ then Result := ghy.Position - 2
   else if hx = ’FF’ then
      ghy.Position := ghy.Position-1;
   end; //if
  end; //while
  finally
   ghy.Free
  end; //try
 end;


procedure TForm1.savebuttonClick(Sender: TObject);
var
picstream:tadoblobstream;
begin
adoquery1.edit;
picstream:=tadoblobstream.Create(tblobfield(adoquery1.fields[1]),bmWrite);
if form1.opendialog1.execute then
begin
picstream.LoadFromFile(opendialog1.filename);
picstream.Position:=0;
adoquery1.edit;
tblobfield(adoquery1.Fields[1]).loadfromstream(picstream);
adoquery1.post;
end;


end;

procedure TForm1.showbuttonClick(Sender: TObject);
var
ghy:TADOBlobstream;
pic:tjpegimage;
begin
ghy := TADOBlobstream.Create(Adoquery1pic, bmRead);
try
ghy.Seek(JpegStartsInBlob(Adoquery1pic),soFromBeginning);
Pic:=TJpegImage.Create;
try
 Pic.LoadFromStream(ghy);
 Image1.Picture.Graphic:=Pic;
finally
 Pic.Free;
end;
finally
ghy.Free
end;
end;

procedure TForm1.DBNavigator1Click(Sender: TObject; Button: TNavigateBtn);
begin
if button in [nbFirst, nbPrior, nbNext, nbLast] then showbutton.Click;
end;

end.

如果数据库中要存储的是BMP文件,则在procedure TForm1.showbuttonClick(Sender: TObject);过程中代码更改如下即可存储显示BMP文件格式的操作。
procedure TForm1.showbuttonClick(Sender: TObject);
var
ghy:TADOBlobstream;
pic:tbitmap;
begin
ghy := TADOBlobstream.Create(Adoquery1pic, bmRead);
try
{ ghy.Seek(JpegStartsInBlob(Adoquery1pic),soFromBeginning);}
Pic:=Tbitmap.Create;
try
 Pic.LoadFromStream(ghy);
 Image1.Picture.Graphic:=Pic;
finally
 Pic.Free;
end;
finally
ghy.Free
end;
end; 

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.