Writing Data Access Code
This section provides a simple scenario that shows how to write the basic data access code needed to pipe Excel Online data into your application. The code example chains FireDAC components like TFDConnection and TFDQuery and executes a SELECT query with simple GUI components.
Setting Connection Properties
To begin accessing Excel Online through a TFDConnection component and then other FireDAC components, create a connection and link the FireDAC components:
- Initialize the Excel Online component: Add a TFDPhysExcelOnlineDriverLink object to your project.
- Link with a TFDConnection object: Set the DriverName property of the TFDConnection object to the Driver Id of the component, CDataExcelOnline.
- Define connection properties through the TFDPhysExcelOnlineConnectionDefParams object. This object enables you to use Code Insight and compiler syntax checking.
FDConnection1.Name := 'SQLTest'; FDConnection1.DriverName := 'CDataExcelOnline'; with FDConnection1.Params as TFDPhysCDataExcelOnlineConnectionDefParams do begin ... end;
You can also set connection properties through the FDManager function. See Connection Definitions for other ways to define connections.
Execute SQL Queries to Excel Online
You can use the TFDQuery object to execute any SQL statement.
- Set the Connection property of a TFDQuery object to a TFDConnection instance.
- Set the Connected property of the TFDConnection object to True to open the connection.
- Call the Open method to execute SELECT queries with a TFDQuery object.
FDQuery1.Connection := FDConnection1; FDConnection1.Connected := true; FDQuery1.Active := true; FDQuery1.Open('SELECT * FROM Test_xlsx_Sheet1 WHERE Column2 = ''Bob''');To execute data manipulation statements, use the ExecSQL method -- both TFDConnection and TFDQuery have these methods, with slightly different functionality. See Manipulating Data for examples and more information.
See Querying Data for more information on selecting Excel Online data with the TFDQuery class.