Both the Table control (Component) and the query control in Delphi need to have a DatabaseName property. The DatabaseName property can be used to specify the data table path referenced by the control. There are two ways to set the path for the property DatabaseName: the first method is to directly enter the path where the data table is located, and the other method is to use the database alias (Alias). In Delphi, database alias is set in the database engine (BDE), and an alias corresponds to a file path. You can define alias as follows: an alias is a mapping of file paths, and is the abbreviation of file paths. Therefore, using database alias in the property DataBaseName and using file paths are essentially the same, both of which serve as the root path for finding the data table. However, in database application design, if there are many Table controls and Query controls involved, then using alias to reference file paths is very efficient, convenient, and not prone to errors. The reason is obvious: on the one hand, aliases can be accessed directly in the list without requiring input from the keyboard like the file path; on the other hand, aliases can be set very short, and even input from the keyboard is relatively quick.
In addition, the SQL statements of the TableName property of the Table control and the Query control can contain the path information of the data table. Because the situation of Query and Table controls is very similar, the following is only the description of Table controls. This path information can be either a complete path or the subsequent part of a complete path. For example, when assigning the TableName property of a Table control, you can assign c:/delphi/demo1/data/subscriber to it, or you can assign data/subscriber to it. That is, an absolute path can be used or a relative path can be used.
To facilitate program porting, you usually only need to specify the path of the data table in the property DataBaseName, and do not have to include path information in TableName. Generally, do not specify the full path in the property TableName.
Migration of database applications is an important issue. Because in a database application to access a database or data table, the path to the data file must be involved. Generally speaking, the database application must be ported to other machines for use after development, so the data file path when the database application is running is likely to be different from the development time. Therefore, the path of the data file must be effectively managed for system migration. The following are three methods to manage data file paths. 1. Create the data file in a subdirectory of the directory where the application is located. The specific process is (1) For the convenience of design, a database alias can be used during design, and the database alias point to the path where the application is located. Add the subpath name to the TableName property of the Table control. (2) Before issuing the application, set all Table controls and Query space attribute Active to False, and delete all characters in the attribute DataBase (i.e. empty). By default, the current directory at which the application is running is the directory where the application is located. If the process such as ChDir is not called in the application to change the current directory, there is no need to perform the third step, otherwise perform the third step processing.
(3) In the OnCreate event of the form that uses the Table control and Query control, assign the path where the application is located to each Table control and Query control on the form. Table1.databasename:=ExtractFilePath(application.ExeName); 2. Use configuration files to store data paths. This method is to write database path information to the configuration file during the application system installation or the first run. In the future, the path information is read out from the configuration file. The reading and writing of configuration files are implemented using methods provided by the class TiniFile, class TregIniFile and class Tregistry. The basic idea of this method is: (1) For the convenience of design, use database alias during design. (2) Before issuing the application, set all Table controls and Query space attribute Active to False, and delete all characters in the attribute DataBase (i.e. empty).
(3) In the OnCreate event of the form that uses Table controls and Query controls, assign the path where the application is located to each Table control and Query control on the form.
Table1.databasename:=ExtractFilePath(application.ExeName);
3. Use database alias to store database paths
To use session to dynamically create a database alias, you need to add a session1 component to the form. After the addition is successful, add a DBTables unit to the uses clause of the unit interface part of the system corresponding to the form. What we will use when dynamically creating and deleting database alias is a Session global variable defined by the unit, rather than the Session1 component added to the form. During the creation of the test bank system, in order to prevent confusion, we deleted the Session1 component on the form, which has no effect on the implementation of the function.
The following code shows how to create an alias named "Subscriber", its database path is "E:/subscriber" and its database type is PARADOX.
Session.ConfigMode:=cmall;//Specify the configuration mode of Session
Session.AddStandardAlias('net','E:/subscriber','PARADOX'); //Create a paradox-type database with alias net, and its path is e:/subscriber Session.SaveConfigFile; //Save configuration modification
If you need to delete an alias, you must first check whether the alias exists. If you delete an alias that does not exist, an exception will be caused. The following code shows how to check whether the database alias "Subscriber" exists, and if so, delete it.
Define variable: var strAlias:TStringList; // Used to save BDE database alias table
Code: strAlias:=TStringList.Create;//Generate an instance of the TstringList class
Session.GetAliasNames(strAlias);//Get all database alias currently
// Find whether the subscriber exists. The return value of existence is -1, and the non-existence is non-1.
if (strAlias.IndexOf('Subscriber') <> -1) then
Begin
session.DeleteAlias('Subscriber ');//Delete the database alias subscriber
session.SaveConfigFile; //Save
end;