求助~用sqlquery往数据库插入二进制出现内存错误
- Delphi(Pascal) code
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;[解决办法]
- Delphi(Pascal) code
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;