你应该该为image字段类型,不存在该问题

解决方案 »

  1.   

    你应该该为image字段类型,不存在该问题
      

  2.   

    我是通过如下方式知道数据存取前后不一样:
    存之前,
    msBinary := TMemoryStream.Create ;
    msBinary.Write(MyOleVariant, 3072) ;
    msBinary.Position := 0 ;
    msBinary.SaveToFile('Binary1.dat') ;
    存完再取,
    msBinary := TMemoryStream.Create ;
    ......
    TBlobField(Table1.FieldByName('BinaryField')).LoadFromStream(msBinary) ;
    msBinary.Position := 0 ;
    msBinary.SaveToFile('Binary2.dat') ;
    ......
    然后一对比两个文件,发现有很大的不同.
      

  3.   

    就是该用image
    我以前遇到过
      

  4.   

    列位大哥的谆谆教导,让小弟我看到了希望,我先试试Image字段.我想问一下,我该采取我前面写的方法几呢?
      

  5.   

    好象仍然有问题,我的代码如下:
    存:
    msBinary := TMemoryStream.Create ;
    msBinary.Write(MyOleVariant, 3072) ;
    msBinary.Position := 0 ;
    msBinary.SaveToFile('binary1.dat');
    msBinary.Position := 0 ;Table1.Append ;
    Table1.FieldByName('No').AsInteger := 1 ;
    TBlobField(Table1.FieldByName('Binary')).LoadFromStream(msBinary) ;
    Table1.Post ;MsBinary.Free ;取:
    Query1.Close ;
    Query1.SQL.Clear ;
    Query1.SQL.Add('Select Binary From TABLEtest Where No = 1') ;
    Query1.Open ;
      
    msBinary:= TMemoryStream.Create ;
    TBlobField(Query1.FieldByName('Binary')).SaveToStream(msBinary) ;
    msBinary.Position := 0 ;
    msBinary.SaveToFile('binary2.dat');
    msBinary.Free ;
    最后对比binary1.dat和binary2.dat,发现两个文件首先大小就不一样,前一个有3K大,后一个只有1K多.
      

  6.   

    真是怪异!我的代码也没有改,现在binary1.dat和binary2.dat两个文件不同的问题又不存在了.
    敢问列位前辈,TMemoryStream对象中的数据怎么读到OleVariant变量中,以实现SQL Server数据库Image字段到OleVariant变量的存取.
      

  7.   

    下面是一个从sqlserver中存取Image字段的函数,希望能够对你又帮助:
    取:
    function LoadFromBlob(const AField: TField; const Stream: TStream): boolean; 
    var 
      ResultStr: string; 
      PResultStr: PChar; 
    begin 
      Result := false; 
      if (Assigned(AField)) and (Assigned(Stream)) then begin 
        try 
          ResultStr := AField.Value; 
          PResultStr := PChar(ResultStr); 
          Stream.Write(PResultStr^, Length(ResultStr)); 
          Stream.Seek(0,0); 
          Result := true; 
        except 
        end; 
      end; 
    end; 
    存:
    function SaveToBlob(const Stream: TStream; const AField: TField): boolean; 
    var 
      FieldStr: string; 
      PFieldStr: PChar; 
    begin 
      Result := false; 
      if (Assigned(AField)) and (Assigned(Stream)) then begin 
        try 
          Stream.Seek(0,0); 
          SetLength(FieldStr, Stream.Size); 
          PFieldStr := PChar(FieldStr); 
          Stream.Read(PFieldStr^, Stream.Size); 
          AField.Value := FieldStr; 
          Result := true; 
        except 
        end; 
      end; 
    end; 
    例子:存储一个icon文件到对应的字段
    qryBlobTest是一个adoQuery,有一个nFileIcon: Image;字段
    function StoreFileIcon: boolean; 
    var 
      AFileIcon: TIcon; 
      MS: TMemoryStream; 
    begin 
      Result := false; 
      AFileIcon := TIcon.Create; 
      MS := TMemoryStream.Create; 
      try 
        AFileIcon.handle := ExtractAssociatedIcon('c:\temp\Test.doc'); // Pseudocode !! 
        AFileIcon.SaveToStream(MS); 
        Result := SaveToBlob(MS, qryBlobTest.FieldByName('nFileIcon')); 
      finally 
        AFileIcon.Free; 
        MS.Free; 
      end; 
    end; 
    希望能够对你有所帮助!
      

  8.   

    一个分析:我没有用过SQL,但是我想问题会不会出在.dat上,你不用扩展名或用DXT之类肯定没有被别的软件定义过的扩展名,我想知道结果。我的感觉是对ole,windows悄悄地干一些事情,必须避开。
      

  9.   

    流到OleVariant的转换
    function TImgAndDb.StreamToOle(InStream:TStream):OleVariant;
    var
       MSize:integer;
       MyP:pointer;
    begin
       MSize:=InStream.Size;
       Result:=VarArrayCreate([0,MSize-1],varbyte);
       MyP:=VarArrayLock(Result);
       InStream.ReadBuffer(MyP^,MSize);
       VarArrayUnLock(Result);end;