// 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;