When we operate on a database, we often use the ttable control, but ttable can only operate on one datasheet at a time, while the Tquery control not only has most of the ttable functionality, but it can also operate on multiple data tables. Not only that, the Tquery control also has a more powerful database query function, you can simultaneously on a number of different structure of the database to query, the queried database can be any type of ODBC support, of course, the computer must first install ODBC and BDC (Delphi database Engine), Examples are provided.
One, connect Paradox and dBASE to carry on the different library query
For example: Master.db and customer.dbf are Paradox and dBASE databases, which have a public field custno. You can then enter the following code:
Query1.close;
query1.databasename:=′′;
Query1.SQL.Clear;
QUERY1.SQL.ADD (′select * from″master.db″a,″customer.dbf″b Where a.custno=b.custno′);
{Enter in the same line}
Query1.open;
Note: The DatabaseName property of the Tquery control must be empty first, the single quotation mark does not contain any characters, and A and B are master.db and customer.dbf aliases (reference aliases reduce the length of code) when doing a multi-library check.
Second, MS access multiple table joint search
An Access database belongs to a multiple-table collection database (one database contains multiple data tables), so querying two access databases differs from the above. For example: We have Rsda.mdb and zfgjj.mdb two databases, RSDA contains the personnel files, the working staff directory and the employee directory and other data tables, ZFGJJ through the public field "user number" and RSDA connected. If we extract a user number from the ZFGJJ, we need to obtain the name of the employee who owns this user number from the RSDA, then we must use the Tquery multiple table query.
The first step is to add two data sources "ZFGJJ" and "RSDA" to ODBC.
The second step is to add two database column names "ZFGJJ" and "RSDA" to the BDE database engine.
The third step is to add two database controls "DataBase1" and "DataBase2" to the form, select "Zfgjj" and "RSDA" respectively in the AliasName property, and type "DatabaseName" and "ZFGJJ" in the RSDA properties. , and finally set the Keepconnection and connected properties to "True."
Now you can enter the appropriate code:
Begin
For I:=1 To 10 Do
Begin
Query1.Close;
Query1.DataBaseName:=′′;
Query1.Sq1.Clear;
Query1.Sq1.Add(′Select A.七月,B.个人账号,B.用户号,C.姓名From“:ZFGjj:个人汇激部门表”A,“:ZFGjj:住房公积金明细表”B,“:RSDA:在职职工目录”C
Where(A.用户号=B.封存=:ib)′);{在同一行输入}
Query1.Prepare;
Query1.Params[0].DataType.=ftInteger;
Query1.Params[1].DataType.=ftBoolean;
Query1.Params[0].Asinteger:=ia[I];
Query1.Params[1].Asboolean:=ib;
Query1.Open;
End;
End;
The SQL statement in your code must be entered within one line and cannot be branch. The dynamic parameter "IA" and "IB" are used in SQL statements. Delphi automatically adds them to the Query1 params attribute in the order in which they appear and assigns index numbers 0 and 1 respectively, and the argument "IA" and "IB" should declare their type and assign values at the beginning of the program.