With the wide application of the database, database programming has become a rapid development of programming. The powerful feature of C Builder in database development is unparalleled, and you can even generate a beautiful database program without writing a program.
Let's take a look at the use skills of several database VCLs in C Builder:
First, DBGRID control
1. Set the field display width attribute of DBGRID
In order to create a smaller column in DBGRID, you must establish a display title, which is equal to or less than field values. For example, you want to build a column with only three characters wide, your column header shows that there must be only three characters or less.
2. Change the display field and date display format of DBGRID
(1) Double-click the table1 corresponding to the DBGRID to enter the field editor.
(2) Right-click "Add Fields ...", the Add Fields ... ", the Add Field dialog box, select the field you want to add (this field will be displayed by DBGRID when running) and point OK button.
(3) Assumption Add the "Date" field, click this field, in the property table: DISPLAYLABEL Fill in the field name you want DBGRID display. If the original field name is in English, here DBGRID will display the Chinese name with the Chinese name. Fill in DISPLAYFORMAT: YYYY-MM-DD, will be displayed in 1999-05-28 format later.
Second, TQUERY control
The TQuery control is a very important control in database programming. It is responsible for establishing contacts with the database through BDE and the SQL statement is conveniently established. Query must establish the corresponding SQL to take effect.
The parameter setting of TQuery is as follows:
(1) In the SQL attribute: SELECT * FROM table name Where field name =: variable name
Follow the ":" is the variable. After writing, the data type of the variable can be modified in the parameter properties.
(2) Assignment of variables:
Query1-> Active = false;
Query1-> params-> items [0] -> asstring = edit1-> text;
Query1-> Active = true; / / Find records that match the variable
(3) Display the result with DBGRID
DBGRID's DataSource is connected to DataSource1, while DataSource1's DataSet is connected to TQuery1.
Third, the application example
The query established by the QUERY control is easier and more efficient than Table.
Use a simple code to explain how to establish a query program:
For example, to create a program name book1 booking book 1 book 1, add DBGRID, DataSource, Query on the form, add the following code:
DBGRID1-> DataSource = Datasource1;
DataSource1-> DataSet = Tqery1;
Query1-> close ();
Query1-> SQL-> CLEAR ();
Query1-> SQL-> Add ("SELECT * FROM table where (book name = 'BOOK1'");
Query1-> EXECSQL ();
Query-> Active = true;
You can see all names BOK1 records in the generated table.