線上訂房服務-台灣趴趴狗聯合訂房中心
發文 回覆 瀏覽次數:1110
推到 Plurk!
推到 Facebook!

使用TSimpleDataSet讀取及刪除資料的問題

答題得分者是:hagar
apl
一般會員


發表:5
回覆:4
積分:1
註冊:2003-04-08

發送簡訊給我
#1 引用回覆 回覆 發表時間:2004-01-05 11:42:43 IP:61.220.xxx.xxx 未訂閱
我使用TSimpleDataSet欲將TBL_MSG資料表的資料備份至檔案,規則是按時間順序一次備份100筆5分鐘前之資料,備份完畢立即將資料自資料庫刪除。 但每次執行結果,只備份了50筆,請問我的作法有何問題嗎?(ps. TBL_MSG內含數千筆資料, 每筆皆已超過5分鐘) 資料庫:MS SQL Server 2000 / Windows 2000 Server Delphi:7.0 & dbExpress    
procedure BackupMsg;
var
 iLen, iResult: Integer;
 arrMsg: array[0..100] of Byte;
 buf: array of Byte;
 fs: TFileStream;
begin     // 建立新檔案
 fs := TFileStream.Create('c:\test.log', fmCreate);     with dbxsds do  // dbxsds: TSimpleDataSet
 begin
  Active := False;
  PacketRecords := 100;
  DataSet.CommandText := 'select top 100 * from TBL_MSG where (datediff(minute,TIME_RECEIVED,getdate())>5) order by TIME_RECEIVED';
  Active := True;      First;
  iResult := 0;
  while not Eof do
  begin
   iLen := FieldByName('MSG_LENGTH').AsInteger; // 讀出資料長度
   buf := FieldValues['MSG_DATA']; // 讀出資料內容, MSG_DATA: varbinary(100)
   CopyMemory(@arrMsg[0], buf, iLen); // 複製至arrMsg[]陣列
   fs.WriteBuffer(arrMsg, iLen); // 寫入檔案       Delete; // 刪除本筆資料
   Inc(iResult);
   Next;
  end; // while
  ApplyUpdates(0);
  Active := False;     end; //with     ShowMessage(Format('應備份100筆, 實際備份%d筆', [iResult]));    end;
hagar
版主


發表:143
回覆:4056
積分:4445
註冊:2002-04-14

發送簡訊給我
#2 引用回覆 回覆 發表時間:2004-01-05 21:37:43 IP:202.39.xxx.xxx 未訂閱
// ...
  while not Eof do
  begin
   iLen := FieldByName('MSG_LENGTH').AsInteger; // 讀出資料長度
   buf := FieldValues['MSG_DATA']; // 讀出資料內容, MSG_DATA: varbinary(100)
   CopyMemory(@arrMsg[0], buf, iLen); // 複製至arrMsg[]陣列
   fs.WriteBuffer(arrMsg, iLen); // 寫入檔案       Delete; // 刪除本筆資料
   Inc(iResult);
   Next; // 這行拿掉試試
  end; // while
  ApplyUpdates(0);
  Active := False;     end; //with
 // ...
end;
--- Everything I say is a lie. --<-<-<@
apl
一般會員


發表:5
回覆:4
積分:1
註冊:2003-04-08

發送簡訊給我
#3 引用回覆 回覆 發表時間:2004-01-05 21:47:38 IP:218.168.xxx.xxx 未訂閱
可以了, 謝謝!    剛好也看到scottliou兄的說明: http://delphi.ktop.com.tw/topic.php?topic_id=18227
引言: while not eof 迴圈 我記得若Delete一筆資料時下一筆記錄會遞補所以應不須Next; 試看看用 first; while not eof do delete;
系統時間:2024-06-29 16:37:05
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!