高红岩(ghyghost)
近日笔者书写一个小型的学生管理系统时,需要用到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 < ghy.Size) do
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),bmWrite);
if form1.opendialog1.execute then
be