
上QQ阅读APP看书,第一时间看更新
How to do it...
Let's look at the following steps:
- Create a new VCL application and drop these components (every time you add a component, align it to the top)—TComboBox, TButton, TPanel, TDBGrid, TPanel, and TDBGrid (this time, align the component to the client).
- Ensure you perform caption refactoring, adjust the component size, and so on to make your form look like this:

Figure 1.30: Form layout at design time
- If you have followed the instructions of the The Amazing FDTable recipe on database preparation, you should see the database connections, as in Figure 14.1, in the Data Explorer tab under the InterBase entry. Select the DELPHICOOKBOOK and EMPLOYEE connections, and drag and drop the CUSTOMERS table from DELPHICOOKBOOK and the CUSTOMER table from EMPLOYEE onto the form.
- This operation generates four components:
- DelphiCookbookConnection: The FDConnection to DELPHICOOKBOOK
- CustomersTable: The TFDQuery component relating to the CUSTOMERS table
- EmployeeConnection: The FDConnection to Employee
- CustomerTable: The TFDQuery component relating to the CUSTOMER table
- Set these SQL statements to TFDQuery components into the form:
- CustomerTable: select CUST_NO as ID, CONTACT_FIRST as FIRSTNAME, CONTACT_LAST as LASTNAME from {id CUSTOMER}
- CustomersTable: select * from {id CUSTOMERS}
- Put the TFDBatchMove component, and two TDataSource components:
- Rename TDataSource to dsCustomer, set the DataSet property to CustomerTable, and assign it to the DataSource property of the first DBGrid
- Rename the second TDataSource to dsCustomers, set the DataSet property to CustomersTable, and assign it to the DataSource property of the second DBGrid
- We'll use the TCombobox component to allow the user to choose the operation to be performed, so set the Items property as follows:
- CSV to Table
- Table to Table
- Table to CSV
- Declare the CloseDataSets procedure in the private section of the form and use the following code:
procedure TMainForm.CloseDataSets;
begin
CustomersTable.Close;
end;
- Declare the OpenDataSets procedure in private section of the form and use the following code:
procedure TMainForm.OpenDataSets;
begin
CustomersTable.Close;
CustomersTable.Open;
CustomerTable.Close;
CustomerTable.Open;
end;
- Declare the SetUpReader procedure in the private section of the form and use the following code:
procedure TMainForm.SetUpReader;
var
LTextReader: TFDBatchMoveTextReader;
LDataSetReader: TFDBatchMoveDataSetReader;
begin
case ComboBox1.ItemIndex of
0:
begin
// Create text reader
// FDBatchMove will automatically manage the reader instance.
LTextReader := TFDBatchMoveTextReader.Create(FDBatchMove);
// Set source text data file name
// data.txt provided with demo
LTextReader.FileName := ExtractFilePath(Application.ExeName) +
'..\..\data\data.txt';
// Setup file format
LTextReader.DataDef.Separator := ';';
// to estabilish if first row is definition row (it is this case)
LTextReader.DataDef.WithFieldNames := True;
end;
1:
begin
// Create text reader
// FDBatchMove will automatically manage the reader instance.
LDataSetReader := TFDBatchMoveDataSetReader.Create(FDBatchMove);
// Set source dataset
LDataSetReader.DataSet := CustomerTable;
LDataSetReader.Optimise := False;
end;
2:
begin
LDataSetReader := TFDBatchMoveDataSetReader.Create(FDBatchMove);
// set dataset source
LDataSetReader.DataSet := CustomersTable;
// because dataset will be show on ui
LDataSetReader.Optimise := False;
end;
end;
end;
- Declare the SetUpWriter procedure in the private section of the form and use the following code:
procedure TMainForm.SetUpWriter;
var
LDataSetWriter: TFDBatchMoveDataSetWriter;
LTextWriter: TFDBatchMoveTextWriter;
begin
case ComboBox1.ItemIndex of
0:
begin
// Create dataset writer and set FDBatchMode as owner. Then
// FDBatchMove will automatically manage the writer instance.
LDataSetWriter := TFDBatchMoveDataSetWriter.Create(FDBatchMove);
// Set destination dataset
LDataSetWriter.DataSet := CustomersTable;
// because dataset will be show on ui
LDataSetWriter.Optimise := False;
end;
1:
begin
// Create dataset writer and set FDBatchMode as owner. Then
// FDBatchMove will automatically manage the writer instance.
LDataSetWriter := TFDBatchMoveDataSetWriter.Create(FDBatchMove);
// Set destination dataset
LDataSetWriter.DataSet := CustomersTable;
// because dataset will be show on ui
LDataSetWriter.Optimise := False;
end;
2:
begin
LTextWriter := TFDBatchMoveTextWriter.Create(FDBatchMove);
// set destination file
LTextWriter.FileName := ExtractFilePath(Application.ExeName) +
'DataOut.txt';
// ensure to write on empty file
if TFile.Exists(LTextWriter.FileName) then
TFile.Delete(LTextWriter.FileName);
end;
end;
end;
- Now, create event handlers for the Execute button and write the code that follows:
procedure TMainForm.Button1Click(Sender: TObject);
begin
// ensure user make a choice
if ComboBox1.ItemIndex = -1 then
begin
ShowMessage('You have to make a choice');
exit;
end;
CloseDataSets;
// SetUp reader
SetUpReader;
// SetUp writer
SetUpWriter;
// Analyze source text file structure
FDBatchMove.GuessFormat;
FDBatchMove.Execute;
// show data
OpenDataSets;
end;
- Run the application by hitting F9 (or by going to Run | Run).
- In the order they are shown, select the item of TComboBox and click on the ExecuteButton to perform the operation.
- After the third click, you should see something similar to the following screenshot:

Figure 1.31: Customers table after batchmove operations
- In addition, at the same level as the executable file, you should find the DataOut.txt file as follows:

Figure 1.32: Output file generated