最近delphi作一個小工具其中一個需求要把上W張照片存入數據庫多媒體字段。數據庫
程序轉到1,2W的時候即內在溢出了。最多一次轉了3W張照片。很簡單的一段代碼後來仔細檢查發現其中的坑。工具
下面放上代碼spa
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;
注意:code
'SELECT ID, Data, Ext FROM Table where 1=2'; 這段代碼中的where 1=2就是這個where加上這後進程內存一直保持在30M左右不加這個where內在就一直不停增加,直到內存溢出。感受不加這一句,每次添加的照片就增長到blog
DMConn.AdsEdit裏面了。