如何把文件通过流写入sql server 数据库,附源码 马上给分1

解决方案 »

  1.   

    query1.fieldbyname('field').loadfromstream(source);
      

  2.   

    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;
      

  3.   

    // 先把文件保存到FileStream,然后FileStream.Position := 0;
    (ADOQuery1.FieldByName('FileField') as TBlobField).LoadFromStream(FileStream);
      

  4.   

    unit Unit1;interfaceuses
      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.
    把这个好好研究研究!