Using Delphi to develop network database application

Source: Internet
Author: User
Tags rollback how to use sql

Delphi is a visual interface of Object-oriented programming language, it is powerful, simple and easy to use and many other characteristics, by the programmer's welcome. Delphi provides the database engine BDE (Borland db Engine), and contains numerous database call artifacts, which is convenient for programmers to develop client/server applications. Under normal circumstances, using Delphi to develop database applications, you can use Ttable, Tdatasource, Tdbedit, Tdbnavigator and other components. As long as some attributes of the component are set correctly, and then the necessary program code is programmed to deal with certain events, data processing can be done for a variety of databases, such as recording input, modification, deletion and query. This can be done with a very small amount of program code, but if the structure of some of the data tables changes during the programming process, you must modify the properties of all the artifacts associated with the datasheet, which will put the programmer into tedious repetitive work.

To avoid this, we can use Structured Query Language SQL (Structured query Language) when writing a network database application to make it easier to exchange Dynamic data with various back-end databases such as SQL Server and Oracle , and can make changes and porting of programs more flexible. We take the data input module as an example to illustrate how to use SQL statements to implement the data processing function when developing client/server applications. First, determine if a transaction handler is running and, if so, roll it back (rollback), or, if not, start a new transaction to prepare for the final processing of the data. Second, you set up the SQL statement and write it to the Tquery widget. Finally, the transaction is committed or rolled back to complete the data entry of a record.

The following is the process code for entering records into a database, using the contents of the Tedit type edit box in the page as the data source:

procedure Datainsert (const qname:tquery;
Szdbname:string;inum:integer;
Imark:array of Integer;ename:array of Tedit);  
var
I:integer;
szsql:string;  
Begin
If Datamodule1.database1.intransaction=true then
DataModule1.DataBase1.RollBack;
DataModule1.DataBase1.StartTransaction;      
szSQL: = ' INSERT into ' +szdbname+ ' VALUES ('
for i:=0 to INum-1 do
begin
If Imark[i]=0 then
szSQL: = Szsql+ename[i]. Text {Non-character way}
Else
szSQL: = szsql+ ' "' +ename[i]. Text+ ' "; {character mode}    
If I=inum-1 then
szSQL: = szsql+ ') '
Else
szSQL: = szsql+ ', ';
End
Qname.close; {Close Query}
QName.SQL.Clear; {Clear SQL attribute content}
QName.SQL.ADD (szSQL); {Add SQL Content}
szSQL: = ' SELECT * from ' +szdbname;
QName.SQL.ADD (szSQL);
Qname.open; {return result set}
DataModule1.DataBase1.Commit;
End;

The above procedure contains five parameters that enable you to read data from a series of edit boxes in a page and to enter features into the specified data table. Where the parameter qName is the name of the Tquery class widget used in the page, the parameter szdbname is the name of the datasheet, and the parameter ename is an array of tedit types, listing the names of each edit box that contains the data in the page; the parameter inum is the number of fields in the datasheet, which is the The parameter imark is an array of integer types that indicate how the field was entered and, if it is a character, you need to enclose the data at both ends. Note that you should open the database connection before calling this procedure:

DataModule1.DataBase1.Connected: = true;

Disconnect the database connection after the procedure call completes:

AtaModule1.DataBase1.Connected: = false;

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.