转贴自:Delphi Database FAQ
给出了 Paradox - ASCII,和 ASCII - Paradox 的两个范例:
说明:从 ASCII - Paradox,对 ASCII 表的要求为:
格式化的 TXT 表,和与之对应的 SCH 文件,该文件存放字段信息和表的信息。
Q: How can I export data from a Paradox table to an ASCII file?
A: In those instances when you intend to manipulate the content of
a text file as if it has fields, what you need is a schema file which
describes the layout of the text file. Below is some sample code that
you can consider when writing your program:
{ Here we assume InTable is to be copied to an ASCII file. We use
TBatchMove because it's fast. Also, it will automatically create
the schema file }
procedure TForm1.Button1Click(Sender: TObject);
var
InTable, OutTable: TTable;
BatchMove1: TBatchMove;
begin
try
{ Fix the BDE "wait" cursor: }
Screen.Cursors[crSQLWait] := LoadCursor(0,IDC_WAIT);
InTable := TTable.Create(Application);
with InTable do
begin
DatabaseName := 'AMP1LINKS';
TableName := 'ACFTOWN.MISHAPS'; { Oracle OWNER.TABNAME }
end;
OutTable := TTable.Create(nil);
with OutTable do
begin
DatabaseName := 'c:/delphi/files';
TableName := 'Test.Txt';
TableType := ttASCII;
end; { Note: that is NO need to call CreateTable! }
BatchMove1 := TBatchMove.Create(nil);
with BatchMove1 do
begin
Source := InTable;
Destination := OutTable;
Mode := batCopy;
Execute;
end;
finally
if Assigned(OutTable) then OutTable.Free;
if Assigned(BatchMove1) then BatchMove1.Free;
end;
end;
{ Now assume that a schematic file does exist; the text file itself
may
or may not exist. With the schematic file there we can perform
field level
manipulation
}
procedure TForm1.Button2Click(Sender: TObject);
var
oTxt: TTable;
i: Integer;
f: System.Text;
begin
try
oTxt := nil;
if not FileExists('c:/delphi/files/Test.Txt') then begin
AssignFile(f, 'c:/delphi/files/Test.Txt' );
Rewrite(f);
CloseFile(f);
end;
oTxt := TTable.Create(nil);
with oTxt do begin
DatabaseName := 'c:/delphi/files';
TableName := 'Test.Txt';
TableType := ttASCII;
Open;
end;
with InTable do begin
DisableControls;
if not Active then Open;
First;
while not EOF do begin
oTxt.Insert;
{ In this instance the schematic file described the layout
of the text file; and in this example there is in fact
one to one correspondence between the fields in the table
and the logical fields defined in the Schema ".sch" file }
for i := 0 to FieldCount - 1 do
oTxt.Fields.AsString := Fields.AsString;
oTxt.Post;
Next;
end;
end;
finally
InTable.EnableControls;
if Assigned(oTxt) then oTxt.Free;
end;
end;