With the wide application of database, database programming has become a rapid development in program design. C + + Builder is unparalleled in database development, and you can even generate beautiful database programs without writing a single line of programs.
The following is an introduction to the techniques used in several database vcl in C++builder:
One, DBGrid control
1. Set DBGrid Field Display Width property
To create a smaller column in the DBGrid, you must create a display title that is equal to or less than the field value. For example, you want to create a column with only three characters wide, and your column headings must show only three or less.
2. Change DBGrid display field and date display format
(1) Double-click the dbgrid corresponding Table1 and enter the field editor.
(2) The right button appears menu select "Add Fields ...", the Add Field dialog box appears, select the field to add (the field will be displayed at run time by DBGrid) and then click OK.
(3) Suppose you add a date field, point to the field, and in the property sheet: Displaylabel The field name you want DBGrid to display. If the original field name is in English, the DBGrid will display the Chinese name after the Chinese name. Fill in DisplayFormat: Yyyy-mm-dd, later date will be displayed in 1999-05-28 format.
Second, Tquery control
The Tquery control is a very important control in database programming, it is responsible for establishing a connection with the database through the BDE, it is convenient to establish the query through the SQL statement. Query must establish the appropriate SQL to take effect.
The tquery parameters are set as follows:
(1) in the SQL attribute: Select * from table name where field name =: variable name
Followed by ":" is the variable. When you write this, you can modify the data type of the variable in the parameter properties.
(2) Assigning values to variables:
Query1-〉active=false;
Query1-〉params-〉items[0]-〉asstring=edit1-〉text;
query1-〉active=true;//find records that match variables
(3) Display results with DBGrid
The DBGrid datasource is connected to the DataSource1, while the DataSource1 dataset is connected to the Tquery1.
Third, application examples
Queries built by embedding SQL statements through the query control are simpler and more efficient than the table.
Use a simple code to illustrate how to set up the query program:
For example, to create a program that retrieves the title Book1 in table 1, place dbgrid,datasource,query three controls on the form to add the following code:
DBGrid1-〉DataSource=DataSource1;
DataSource1-〉DataSet=Tqery1;
Query1-〉Close();
Query1-〉SQL-〉Clear();
Query1-〉SQL-〉Add(″Select * From 表 Where (书名=′book1′ ″);
Query1-〉ExecSQL();
Query-〉Active=true;
You can see all the records named Book1 in the resulting table.