用delphi批量导入某子目录下所有JPG图片文件到数据库
2004-11-20 16:36
731 查看
//保存图片到数据库
function imagesavetosql(dataset:TQuery;filename:String):boolean;
var
imagejpg:TJPEGImage; //jpg图片
MyStm:TMemoryStream;
begin
result:=false;
MyStm:=TMemoryStream.Create;
imagejpg:=Tjpegimage.Create;
if filename<>'' then
begin
imagejpg.LoadFromFile(filename);
imagejpg.SaveToStream(MyStm);
MyStm.Position:=0;
TBlobField(dataset.FieldByName('pict')).LoadFromStream(MyStm); //不可与DMImage之类控件,因为其只支持BMP
result:=true;
end;
MyStm.Free;
imagejpg.free;
end;
//界面上添加TDirectoryListBox,TGauge控件; chk_road(注:checkbox控件)
procedure Tfrmpict.BitBtn2Click(Sender: TObject);
var
dirlist:TStringList;
i:Integer;
begin
inherited;
if chk_road.Checked then
begin
dirlist:=TStringList.Create ;
dirlist.Clear;
try
GetAllFileName(DirectoryListBox1.Directory,dirlist);
if dirlist.Count>0 then
begin
Gauge1.MinValue :=0;
Gauge1.MaxValue:= dirlist.Count-1;
for i:=0 to dirlist.Count-1 do
begin
with dm.qry_pict do
begin
Insert;
Fields[0].AsString:=copy(dirlist.Strings[i],1,pos('.',dirlist.Strings[i])-1);
if imagesavetosql(dm.qry_pict,dirlist.Strings[i] )=false then
begin
ShowMessage('导入'+dirlist.Strings[i]+'.jpg图片时出错');
Abort;
end;
Post;
end; //with
Gauge1.AddProgress(1);
end; //for
end //if
else
ShowMessage('该目录下不存在JPG类型图片');
finally
dirlist.Free;
end;
end
else
ShowMessage('请执行路径选取操作');
//Close;
end;
function imagesavetosql(dataset:TQuery;filename:String):boolean;
var
imagejpg:TJPEGImage; //jpg图片
MyStm:TMemoryStream;
begin
result:=false;
MyStm:=TMemoryStream.Create;
imagejpg:=Tjpegimage.Create;
if filename<>'' then
begin
imagejpg.LoadFromFile(filename);
imagejpg.SaveToStream(MyStm);
MyStm.Position:=0;
TBlobField(dataset.FieldByName('pict')).LoadFromStream(MyStm); //不可与DMImage之类控件,因为其只支持BMP
result:=true;
end;
MyStm.Free;
imagejpg.free;
end;
//界面上添加TDirectoryListBox,TGauge控件; chk_road(注:checkbox控件)
procedure Tfrmpict.BitBtn2Click(Sender: TObject);
var
dirlist:TStringList;
i:Integer;
begin
inherited;
if chk_road.Checked then
begin
dirlist:=TStringList.Create ;
dirlist.Clear;
try
GetAllFileName(DirectoryListBox1.Directory,dirlist);
if dirlist.Count>0 then
begin
Gauge1.MinValue :=0;
Gauge1.MaxValue:= dirlist.Count-1;
for i:=0 to dirlist.Count-1 do
begin
with dm.qry_pict do
begin
Insert;
Fields[0].AsString:=copy(dirlist.Strings[i],1,pos('.',dirlist.Strings[i])-1);
if imagesavetosql(dm.qry_pict,dirlist.Strings[i] )=false then
begin
ShowMessage('导入'+dirlist.Strings[i]+'.jpg图片时出错');
Abort;
end;
Post;
end; //with
Gauge1.AddProgress(1);
end; //for
end //if
else
ShowMessage('该目录下不存在JPG类型图片');
finally
dirlist.Free;
end;
end
else
ShowMessage('请执行路径选取操作');
//Close;
end;
相关文章推荐
- [转帖]Delphi 函数&属性表
- delphi制作的托盘程序
- Delphi字符串函数大全
- 2003年Delphi Informant杂志读者选择奖
- Delphi 快捷键
- 有趣,看看Delphi的property和C#的field
- 重新温习Delphi,真是郁闷死
- 我的现阶段学习任务
- Delphi 控制Excel
- 蛙蛙推荐:Delphi 6 程序员代码编写标准指南
- What's New in Delphi 2005
- CPU信息的获得
- Borland要出Dlephi 2005!Delphi vs C#(老话题:))
- DELPHI中的拖动开发(2)
- DELPHI中拖放的操作
- Delphi资源
- Delphi 2005抢先预览
- Delphi面向对象编程的20条规则
- Delphi面向对象编程的20条规则
- Delphi写的SQL Server扩展存储过程