18.3.1 use tdatabase part to join SQL Server
18.3.1.1 tdatabase Parts Overview
Tdatabase the connection of a part-processing application to a single database. If you do not need to control the database join, you do not have to create the tdatabase part. A temporary tdatabase part is created automatically when an application attempts to open a database table (tables). But if you want to control the continuous connection of the database, the registration of the database server and the value of the database alias or transaction control, you must create a tdatabase part for each desired join.
1. Create Tdatabase Parts
The Tdatabase widget is on the data access page in Component palette, and you can drag and drop it in a database module or form. The tdatabase part is created at design time, and the user can set the initial value and write the Onlogin event-handling procedure (an event Handle). The Onlogin event gives users the ability to customize server security parameters, such as passwords, when registering the database server for the first time.
2. Key attributes of Tdatabase
⑴databasename Property
DatabaseName is the name of the database to be joined and is used for the dataset software, which appears in the Drop-down list box of the DatabaseName property of the dataset part. Setting the DatabaseName property is a specific alias that defines the application of the database. The dataset part can refer to the name to replace the direct use of the BDE alias. This property cannot be modified when the Tdatabase part's Connected property is true.
⑵alianame Property
AliasName is the name of the BDE alias defined by the BDE Configuration tool. Tdatabase gets its default settings from. If you set the DriveName property, the property is cleared and an exception is thrown if you force the DriveName property to be set when the connected is true.
⑶drivename Property
DriveName are the names of BDE drivers, such as standard, ORACLE, SYBASE, Informix, or InterBase. If AliasName is set, the property value is cleared.
⑷params Property
The params property contains the parameters required to open a database on a SQL Server. By default, these parameters are set by the BDE Configuration tool, or the user can modify them with the Database parameter editor (db Parameters Editor). For the database server, params will describe a series of parameters, such as the server name, database name, user name, and password.
⑸connected Property
The Connected property indicates whether a join of the database is established and connected is set to True when the application opens a table in the database, whereas the database table is closed, and connected is set to False unless Keepconnection is true. If you set the connected to true, you can establish a database join without opening the database table. The Tdatabase Keepconnection property describes whether to maintain a database connection when no tables are open in the database.
⑹keepconnection Property
The Keepconnection property describes whether you want to maintain a connection to the server when a table is not open in the database, and it is useful to set the keepconnection to True if the database application needs to open and close multiple tables in a single database, so that even if no tables are open, The application can still maintain a connection to the database, and it can open and close the database tables repeatedly without having to repeat the join process. If Keepconnection is set to False, the database must perform the registration process each time the connected is placed to true.
⑺loginprompt Property
The Loginprompt property is used to control how SQL database security issues are handled. If true, the Standard Delphi Registration dialog box appears on the screen when an application attempts to establish a database join. The user must enter the correct username and password. If set to False, the application looks for the registration parameters in the params property of the tdatabase part. The following are examples of username and password parameters:
USERNAME = SYSDBA
PASSWORD = Masterkey
⑻transisolation Property
The Transisolation property describes all the transaction control independence levels of the SQL Server. Tidirtyread all changes are returned, regardless of whether the record has been committed. Tireadcommitted will only return the submitted records, and the submitted changes will not be reflected in the results. Tirepeatableread will only return the original record during the transaction, even if another application commits the modification.
The various database servers may support these independent levels in varying degrees, or they are not supported at all. If the required level of independence is not supported by the server, then Dephi will use the next higher level of independence, as shown in the following table:
Table 18.10 Various types of server transisolation settings
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
Independent level Oracle Sybase and InterBase
Microsoft SQL
──────────────────────────────────────
Dirty Read Read Committed Read Committed Read Committed
Read Committed Read Committed Read Committed Read Committed
REPEATABLE READ REPEATABLE READ not supported REPEATABLE READ
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
See table 18.12 for the meaning of each individual level.
3. Key methods of Tdatabase
⑴starttransaction method
The Starttransartion method starts transaction control under the independent level specified by the Taransisolation property. If the method is invoked when a transaction has been activated, Delphi throws an exception.
After the method is invoked, modifications to the database are maintained by the database server until the Commmit method is invoked or the rollback method is invoked to cancel the modification. This method can only be invoked when the database server is joined.
⑵rollback method
The Rollback method returns the current transaction control and cancels all modifications made to the database since the most recent call to StartTransaction.
⑶commit method
The Commit method commits the current transaction control and stores all data modifications to the database since the most recent call to StartTransaction.
4. Treatment of Onlogin events in Tdatabase
The trigger condition for the Onlogin event is when the tdatabase part of the join SQL database is opened and the Loginprompt property is true. You can use the Onlogin event handling procedure to set registration parameters at run time. The Onlogin event-handling process gets the tdatabase array of registry parameters params and uses the values property to change these parameters.
For example:
loginparams.vaiues[' SERVER NAME ': = ' myservername ';
loginparams.values[' USER NAME ']: = ' myusername ';
Loginparams.values[password ']: = ' Myapssword ';
When the control is returned from the Onlogin event processing, the application uses these parameters to establish the join.
The declaration of the Onlogin event handling process is this:
Tloginevent = procedure (database:tdatabase; loginparam:tstrings) of Object;
Property onlogin:tloginevent;
The tloginevent type is the method header that handles the Onlogin event. The database parameters are the databases to be joined. Loginparams are tstrings type objects that contain user names and passwords, as well as other parameters that are used to open the database. The username is a string that is shaped like the user name = John.doe, and the password is a string that is shaped like password = Is_password. The user name and password should be added to the loginparams when the Onlogin event process is invoked.