Delphi Perfect Classic-16th Chapter Delphi Database Programming----using BDE components

Source: Internet
Author: User

The 16th chapter of Delphi Database Program design----using BDE components

Delphi accesses the database in the following ways: ADO, BDE, dbexpress, InterBase Express.

One, Tdataset components

Although Delphi has several ways to access the database, they must rely on tdataset. It is used to display all records obtained from a single or multiple data tables in a database.

1. Tdataset Common Properties

Active: Specifies or gets whether the dataset is open. Is true, the equivalent of calling open.

Bof, Eof:bof detects whether the dataset is stopped in the first record. EOF is used to detect whether a dataset is parked in the last record.

BOF is true when any of the following conditions are established

EOF is true when any one of the following conditions has been established

Open DataSet

Open an empty dataset

Call First

Call last (unless the dataset is one-way)

Call Prior, and fails when the first one has been reached

Call next and fail when the last one has been reached

Call SetRange when an empty dataset or selection is obtained

Call SetRange when an empty dataset or selection is obtained

BOF and EOF are true only if the dataset is empty.

Bookmark: When the dataset is not unidirectional, the Bookmark property can be used to specify or take the position of the current record pointer, so that the program can easily return to the specified location.

Canmodify: Detects whether the dataset allows writes.

FieldCount: Gets the number of fields.

Fileddefs: Gets the field definition: Field name, data type, size, and so on.

Fields: Get the corresponding Tfield control through the field array index value, accessing the corresponding field data, its common properties and events:

Øasboolean, Asdatatime, asstring, Asvariant, and so on: Convert field Values to Boolean, Datatime, String, variant, and so on.

Øvalue: Direct access to field value content.

Øongettext Event: You can do some extra processing before the field data is displayed. Its approach is divided into two steps: First write a handler function (the handler has a specific format, reference API), and then assign the handler to a field (for example: Query1.fields[0]. Ongettextmetrics: = handler function).

Øfieldvalue: Sets or gets the variant value of the field, and the input parameter is the field name. such as query1.fieldvalue[' name ']

Øfilter, filtered:filtered switch filter conditions. Filter is used to get or set the current filter string. When filtered is false, the filter is not valid. Filter setting Method: ' NAME = ' mwj '

Østate:dataset the current state. It is used to determine the current mode of operation of the dataset and the behavior that can be done next. Specific reference API.

2. Common methods of Tdataset

Append and Appendrecord:append added empty records at the end of the dataset. Appendrecord added empty records at the end of the dataset. and is saved to the database according to the input values parameter array.

Insert and Insertrecord:insert add empty records. Insertrecord add a record and complete the post.

Cancel: Cancel can discard the change as long as the dataset is in edit mode and has not yet been post.

Post: Log back to the database.

Close: The same effect as active set to false.

Fieldbyname: Obtain the corresponding field by entering the name of the parameter field.

Lookup: Does not move the record pointer to the found record after it is found.

Locate: Finds the current record, returns True when found, and moves the record pointer to the found record. When looking, be careful first.

First, last, Next, prior and find, find last, find Next, Findprior: Back four, with return values. Returns True when found.

Freebookmark, GetBookmark, Gotobookmark:getbookmark specifies that the current record is a bookmark, Gotobookmark navigates to the specified bookmark, and Freebookmark releases the bookmark.

3. Tdataset Common Events

Onfilterrecord: Triggered when filtered is true and the filtered result dataset is not empty.

Seemingly unable to use Tdataset directly, unable to establish Tdataset object. Yes, Tdataset is the common parent of ttable, Tquery, Tstoreproc, and non-BDE data access Components. To use Tdataset, you must pass its subclass functionality.

Second, ttable components

Ttable accesses the database's "single" data table via BDE, which inherits from Tdataset. Most of the properties and aspects have been previously described in Tdataset and only describe the ttable-specific sections.

Common Properties:

Exclusive: A data table used to lock databases such as Paradox, dBASE, and so on, which must be used when the ttable is closed. Not all databases support exclusive operations.

Indexdefs: Establishes and maintains the index of the table.

TableName: Sets or gets the data table name.

Common methods:

Batchmove: Moves a record from one dataset to another table. The database has an index key when it is used.

CreateTable: Create a data table. Defines the field name, type, and so on before calling through the fielddefs.addfielddef of the dataset.

Deletetable: Close table before deleting.

Third, Tquery control

Tquery inherits from Tdataset, which is used to access a single or multiple data tables of a database through SQL instructions.

Common Properties:

Datasource:datasource is used to automatically fill in the corresponding value of the field with the same name as the parameter.

SQL: Used to set up, fetch SQL instructions.

Text: Read-only. Gets the SQL string.

Paramcheck: Set to False to prevent query from taking SQL strings as parameters. If SQL contains a time value (15:20:39), set to False to avoid the colon being treated as a parameter.

Params: Sets or gets the parameter name of the query, the parameter value, and the data type of the parameter.

Requestlive: Set to True to change the database directly through the dataset result of select.

Common methods:

Execsql: An action query that executes a SQL instruction (INSERT, UPDATE, delete, and so on). To execute a SELECT, call the Open method.

Parambyname: The parameter of the query is manipulated by the parameter name.

Prepare, Unprepare:prepare improve the execution efficiency of query, unprepare to close Prepare. Without calling, the query component has automatically switched prepare for us.

Tquery use: You need to set databasename and SQL statements so that their active is true when set. If you change the SQL statement, you need to reset active to true.

Iv. Tdatabase Components

The Tdatabase object manages the BDE schema application that connects to a single database, which is commonly used to handle events or log on to the database settings.

Common Properties:

AliasName, DatabaseName, Drivername:aliasname Read and write. Database alias for the attached BDE. DatabaseName: Database name. If the databasename is the same as aliasname, you do not need to set aliasname and drivername. DriverName: Database drive name. The connected must be false before the change.

Connected, keepconnection:connected: Read and write. Whether the database is online. Keepconnection: Specifies whether the program keeps the online database, even if the dataset is not open.

Exclusive: When a program uses a database, setting up Exclusive locks the database and avoids other programs from using it. Not all databases support this property.

Loginprompt: Set to True, login to the database will appear the account, Password prompt dialog box.

Intransaction: Checks whether the database event is being processed.

Transisolation (Isolation: Isolation): The isolation layer of an event, which is used to set the relationship of interactions between events. Not all databases are supported. Value:

Tidirtyread

Allow reading of content that has not been changed by other events

tireadcommitted

Default value, allowing reading of content that other events have changed

Tirepeatableread

The most stringent isolation layer. It guarantees that other events after reading do not alter the current data (unless the current event itself alters the recorded data).

Common methods:

Applyupdates: When specifying a dataset's

Closedatasets: Closes all datasets, but does not close the database connection. Close closes the database connection.

Commit: Confirms that the current event is changed (added, updated, deleted), and ends the current event. You must first confirm the existence of the current event with Intransaction before calling commit, or you will get an error.

Rollback: Cancels all new, updated, deleted operations in the event and ends the current event.

Common events:

Onlogin: When Loginprompt is true, connecting to the database triggers the Onlogin event.

Ttable Control Example: Establish a one-to-many relationship between two data tables: commonly known as master/detail. If the order system is a one-to-many relationship, a customer will have a few data, but an order will only correspond to a customer profile.

Two Tdatasource, two ttable, two Tdbgrid. Set the DataSource1 dataset to Table1. DBGrid1 's DataSource is set to DataSource1. Now that the Master data table has been set, set the detail data table: Table2 mastersource set the corresponding attribute to Datasource1,masterfields. Then the DataSource2 dataset is set to Table2,dbgrid2 and DataSource is set to DataSource2. When the cursor moves over the data on the DBGRID1, the DBGRID2 data changes accordingly. However, the DBGRID1 does not change when the cursor moves over the DBGRID2. Note that the Tdblookuplist control is distinguished from Chapter 18 (This control changes in both directions and the Master/detail changes only one way).

http://blog.csdn.net/mwj_88/article/category/1426754

Delphi Perfect Classic-16th Chapter Delphi Database Programming----using BDE components

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.