function TServerMethods1.UploadPic(uName: string; uPortrait: TStream): Boolean;
//上传图片
const
  BufSize = 51200;
var
  Stream: TStream;
  MS: TMemoryStream;
  Buffer: TBytes;
  ReadCount: Integer;
begin
  Result := False;
  Stream := uPortrait;
  try
    MS := TMemoryStream.Create;
    if Stream.Size < -1 then
    begin
      SetLength(Buffer, BufSize);
      repeat
        ReadCount := Stream.Read(Buffer[0], BufSize);
        if ReadCount > 0 then
          MS.Write(Buffer[0], ReadCount);
      until ReadCount < BufSize;
    end
    else
      MS.CopyFrom(Stream, 0);
      MS.Position := 0;
      MS.SaveToFile('c:\users\fky\desktop\123.bmp');
     // 上面的都ok
    try
      with sqlqry_Pic do
      begin
        Close;
        SQL.Clear;
        SQL.Add('update t_User set u_Portrait = :p_Portrait where u_Name = :p_Name');
        TBlobField(Params.ParamByName('p_Portrait')).LoadFromStream(MS);
       //  就是到上面这行,出现内存错误了
        Params.ParamByName('p_Name').Value := uName;
        ExecSQL(False);
        Result := True;
      end;
    except
      Result := False;
    end;
  finally
    MS.Free;
  end;
end;

解决方案 »

  1.   

    读取我已经做完了, 就是这个写入,已经写到内存流中了, MS.SaveToFile('c:\users\fky\desktop\123.bmp');这个也显示正确,就是往数据库里写不对头
      

  2.   


      SQL.Clear;
      SQL.Add('update t_User set u_Portrait =:p_Portrait where u_Name = :p_Name');
      // 加一行,刷新一下参数,好像是Paramters还是Params自己试
      Params.Refresh;
      TBlobField(Params.ParamByName('p_Portrait')).LoadFromStream(MS);
      //  就是到上面这行,出现内存错误了
      Params.ParamByName('p_Name').Value := uName;
    还有种方式就是,全部以SQL字串的方式来写,如:update tablename set image_field=0x010203 where image_id = id_value将Stream转换成'0x010203'类似的字符串,拼成上述格式,然后执行。
      

  3.   

    是不是sqlquery的原因,用ado能插入但如果是这个的原因,那连数据库用的是db,如何解决。。