MySQL + Unidac transaction example

Source: Internet
Author: User
// Note: MySQL must use the InnoDB engine to support the transaction function. Otherwise, the following transaction-related code will be invalid.

// SQL Server and Oracle have not been tried yet. I will go back to the company tomorrow and try again.

Procedure tform1.btn1click (Sender: tobject );
VaR
I, J: integer;
Begin
Uniconnection1.connect;
Try
Try
Unitransaction1.connections [0]. starttransaction;
Uniquery1.params. Clear;
Uniquery1. SQL. Text: = 'insert into user (username, password) values (: username,: password )';
Uniquery1.params. parambyname ('username'). Value: = 'eboy ';
Uniquery1.params. parambyname ('Password'). Value: = '000000 ';
Uniquery1.execsql; // if no transaction is used, this entry is inserted.

Uniquery1.params. Clear;
Uniquery1. SQL. Text: = 'insert into user (username, password) values (: username,: password )';
Uniquery1.params. parambyname ('username'). Value: = 'frr ';
Uniquery1.params. parambyname ('Password'). Value: = '000000 ';
Uniquery1.execsql; // if no transaction is used, this entry is inserted.

Uniquery1.params. Clear;
Uniquery1. SQL. Text: = 'insert into user (username, password) values (: username,: password )';
Uniquery1.params. parambyname ('username'). Value: = 'lj ';
Uniquery1.params. parambyname ('Password'). Value: = '000000 ';
Uniquery1.execsql; // if no transaction is used, this entry is inserted.

Uniquery1.params. Clear;
Uniquery1. SQL. Text: = 'insert into user (username, password) values (: username,: password )';
Uniquery1.params. parambyname ('username'). Value: = 'lj ';
Uniquery1.params. parambyname ('Password'). isnull; // The field is not null, so an error occurs.
Uniquery1.execsql; // after the transaction is started, all the above three will be rolled back.

Unitransaction1.connections [0]. commit;

Except t on E: exception do
Begin
Unitransaction1.connections [0]. rollback;
Showmessage (E. Message );
End;
End;
Finally
Uniconnection1.disconnect;
End;
End;

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.