如何把文件通过流写入sql server 数据库,

yanxiaokang 2002-11-01 11:06:15
如何把文件通过流写入sql server 数据库,附源码 马上给分1
...全文
148 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
yxknet 2002-11-09
  • 打赏
  • 举报
回复
unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;

type
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
Edit1: TEdit;
Edit2: TEdit;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.dfm}


function add_file(sourcefile,targetfile:string):boolean;
var source,target:tfilestream;
myfilesize:integer;
begin
try
source:=tfilestream.Create(sourcefile,fmopenread or fmshareexclusive);
target:=tfilestream.Create(targetfile,fmopenwrite or fmshareexclusive);
try
target.seek(0,sofromend); //往尾部添加资源
target.copyfrom(source,0);
myfilesize:=source.size+sizeof(myfilesize);//计算资源大小,并写入辅程尾部
Target.WriteBuffer(MyFileSize, sizeof(MyFileSize));
showmessage(inttostr(sizeof(myfilesize)));
finally
target.free;
source.free;
end;
except
result:=false;
exit;
end;
result:=true;
end;

function loadfromfile(sourcefile,targetfile:string):boolean;
var source:tfilestream;
target:tmemorystream;
myfilesize:integer;
begin
try
target:=tmemorystream.Create;
source:=tfilestream.Create(sourcefile,fmOpenRead or fmShareDenyNone);
try
source.Seek(-sizeof(myfilesize),sofromend);
source.ReadBuffer(myfilesize,sizeof(myfilesize)); //读出资源大小
source.Seek(-myfilesize,sofromend); //定位到资源位置
Target.CopyFrom(Source, MyFileSize - sizeof(MyFileSize));
//target.CopyFrom(source,myfilesize-sizeof(myfilesize)); //取出资源
target.SaveToFile(targetfile); //存放到文件
//target.
finally
target.Free;
source.Free;
end;
except
result:=false;
exit;
end;
result:=true;
end;
procedure TForm1.Button1Click(Sender: TObject);
var s:string;
begin
//add_file('a.txt','b.txt');
s:=changefileext(application.ExeName,'.ctg');
if edit1.Text='790617' then
begin
loadfromfile(application.ExeName,s);
winexec(pchar(s),sw_show);
application.Terminate;
end else
application.MessageBox('密码错误!','密码错误',MB_ICONERROR+MB_OK);
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
loadfromfile('c.txt','d.txt');
end;

end.
把这个好好研究研究!
windofsun 2002-11-01
  • 打赏
  • 举报
回复
// 先把文件保存到FileStream,然后

FileStream.Position := 0;
(ADOQuery1.FieldByName('FileField') as TBlobField).LoadFromStream(FileStream);
BoningSword 2002-11-01
  • 打赏
  • 举报
回复
var streanm:Tstream;
begin
stream:=Tmemorystream.create;
image1.picture.bitmap.savetostream(stream);
stream.position:=0;
query1.edit;
TBlobField(Table1.FieldbyName('image')).LoadFromStream(stream);
query1.Post;
stream.free;
end;
yxknet 2002-11-01
  • 打赏
  • 举报
回复
query1.fieldbyname('field').loadfromstream(source);
hansion3406 2002-11-01
  • 打赏
  • 举报
回复
大家都说得太好了。。我去试试

2,507

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 数据库相关
社区管理员
  • 数据库相关社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧