delphi批量存入多媒体字段 遇到内存溢出的坑

最近delphi做一个小工具其中一个需求要把上W张照片存入数据库多媒体字段。

程序转到1,2W的时候即内在溢出了。最多一次转了3W张照片。很简单的一段代码后来仔细检查发现其中的坑。

下面放上代码

 1   with DMConn.AdsEdit do
 2   begin
 3     Active := False;
 4     CommandText := 'SELECT ID, Data, Ext FROM Table where 1=2';
 5     Active := True;
 6   end;
 7   
 8   with DMDst.UniQuery do
 9   begin
10     Active := False;
11     SQL.Clear();
12     SQL.Add(vSql);      
13     Active := True;  
14     while not Eof do
15     begin
16       vFile := vPath + DMDst.UniQuery.FieldByName('PATH').AsString;
17       vNewID := DMDst.UniQuery.FieldByName('NewID').AsString;
18       gg.Str.Replace(vFile, '/', '\');
19       vPersonID := ExtractFileNameNoExt(vFile);
20 
21       if FileExists(vFile) and (DMConn.GetSQLValueInt('SELECT count(*) FROM Table where ID = ' + QuotedStr(vNewID)) <= 0) then
22       begin
23         try
24           with DMConn.AdsEdit do
25           begin
26             Edit;
27             Append;
28             FieldByName('ID').AsString := vNewPersonID;
29             FieldByName('Ext').AsString := ExtractFileExt(vFile);
30             TBlobField(FieldByName('Data')).LoadFromFile(vFile);
31             Post;
32           end;
33         finally
34         end;
35       end;
36       Next;
37     end;

注意:

'SELECT ID, Data, Ext FROM Table where 1=2'; 这段代码中的where 1=2就是这个where加上这后进程内存一直保持在30M左右不加这个where内在就一直不停增长,直到内存溢出。感觉不加这一句,每次添加的照片就增加到

DMConn.AdsEdit里面了。

转载于:https://www.cnblogs.com/DevMuYuer/p/10177320.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值