Delphi不會寫最後幾行。
一個過程爲每一行調用writeToText(a1, a2, ...)
。Delphi無法正確終止I/O進程
type
TQuerySQL = class
public
Constructor Create;
Destructor Destroy; override;
procedure writeToText(plate:String; positionNo:String; newOdometer:String; oldOdometer:String);
end;
var
tconnect : TADOConnection;
tquery : TADOQuery;
F_Text: TextFile;
implementation
Constructor TQuerySQL.Create;
begin
AssignFile(F_Text, 'results.txt');
ReWrite(F_Text);
tquery := TADOQuery.Create(nil);
tconnect := TADOConnection.Create(nil);
tconnect.Close;
tconnect.ConnectionString := 'Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=SOFT20150911;';
tconnect.ConnectOptions := coConnectUnspecified;
tconnect.LoginPrompt := False;
tconnect.Open;
ShowMessage('starting');
end;
Destructor TQuerySQL.Destroy;
begin
tquery.Close;
tquery.Free;
tconnect.Close;
tconnect.Free;
CloseFile(F_Text);
ShowMessage('finished in destructor');
inherited Destroy;
end;
procedure TQuerySQL.writeToText(plate:String; positionNo:String; newOdometer:String; oldOdometer:String);
begin
Write(F_Text, 'UPDATE POSITIONS SET');
Write(F_Text, ' ODOMETER = '+oldOdometer);
Write(F_Text, ' WHERE');
Write(F_Text, ' LABEL = '''+plate+'''');
Write(F_Text, ' AND POSITIONNO = '''+positionNo+'''');
Write(F_Text, ' AND ODOMETER = '+newOdometer+';');
WriteLn(F_Text);
end;
樣本輸出:
UPDATE SET倉= ODOMETER WHERE 904068,1 LABEL = '34 XXX 11' 和POSITIONNO =「12110126'AND ODOMETER = 904092,2;
UPDATE POSITIONS SET ODOMETER = 904068,1 WHERE LABEL = '01 AL 101'AND POSITIONNO ='165556'AND ODOMETER = 904092,2;
SET ODOMETER UPDATE位置= 904148,7
爲什麼不是你F_Text你TQuerySql類的字段:
任務可以這樣實現的呢?把它作爲一個獨立的全球變量似乎會引起意外誤用。此外,您在執行AssignFile時未指定路徑。順便說一句,你的標題應該是「我不能......」而不是「德爾福不能......」; =) – MartynA
請提供一個[mcve] –
我假設你看到消息「在析構函數中完成」屏幕? –