Representing the Records in a Table

From RAD Studio
Jump to: navigation, search

Go Up to Specifying What Data to Display


When you want to represent all of the fields and all of the records in a single underlying database table, you can use either TSQLDataSet or TSQLTable to generate the query for you rather than writing the SQL yourself.

Note: If server performance is a concern, you may want to compose the query explicitly rather than relying on an automatically-generated query. Automatically-generated queries use wildcards rather than explicitly listing all of the fields in the table. This can result in slightly slower performance on the server. The wildcard (*) in automatically-generated queries is more robust to changes in the fields on the server.

Representing a table using TSQLDataSet

To make TSQLDataSet generate a query to fetch all fields and all records of a single database table, set the CommandType property to ctTable.

When CommandType is ctTable, TSQLDataSet generates a query based on the values of two properties:

  • CommandText specifies the name of the database table that the TSQLDataSet object should represent.
  • SortFieldNames lists the names of any fields to use to sort the data, in the order of significance.

For example, if you specify the following:

SQLDataSet1.CommandType := ctTable;
SQLDataSet1.CommandText := 'Employee';
SQLDataSet1.SortFieldNames := 'HireDate,Salary'
SQLDataSet1->CommandType = ctTable;
SQLDataSet1->CommandText = "Employee";
SQLDataSet1->SortFieldNames = "HireDate,Salary"

TSQLDataSet generates the following query, which lists all the records in the Employee table, sorted by HireDate and, within HireDate, by Salary:

select * from Employee order by HireDate, Salary

Representing a table using TSQLTable

When using TSQLTable, specify the table you want using the TableName property.

To specify the order of fields in the dataset, you must specify an index. There are two ways to do this:

  • Set the IndexName property to the name of an index defined on the server that imposes the order you want.
  • Set the IndexFieldNames property to a semicolon-delimited list of field names on which to sort. IndexFieldNames works like the SortFieldNames property of TSQLDataSet, except that it uses a semicolon instead of a comma as a delimiter.

See Also