//Note: MySQL must use the InnoDB engine to support transactional functionality, otherwise the following transaction-related code will be invalidated. SQL Server and Oracle haven't tried yet, and the company tried to send the code back tomorrow. Procedure Tform1.btn1click (sender:tobject);varI, J:integer;beginUniconnection1.connect; Try Tryunitransaction1.connections[0]. StartTransaction; UniQuery1.Params.Clear; UniQuery1.SQL.Text:='INSERT INTO User (Username,password) VALUES (: Username,:p assword)'; UniQuery1.Params.ParamByName ('username'). Value: ='Eboy'; UniQuery1.Params.ParamByName ('Password'). Value: ='1111'; Uniquery1.execsql; //If you do not use transactions, this entry is inserted into theUniQuery1.Params.Clear; UniQuery1.SQL.Text:='INSERT INTO User (Username,password) VALUES (: Username,:p assword)'; UniQuery1.Params.ParamByName ('username'). Value: ='FRR'; UniQuery1.Params.ParamByName ('Password'). Value: ='123'; Uniquery1.execsql; //If you do not use transactions, this entry is inserted into theUniQuery1.Params.Clear; UniQuery1.SQL.Text:='INSERT INTO User (Username,password) VALUES (: Username,:p assword)'; UniQuery1.Params.ParamByName ('username'). Value: ='LJ'; UniQuery1.Params.ParamByName ('Password'). Value: ='880414'; Uniquery1.execsql; //If you do not use transactions, this entry is inserted into theUniQuery1.Params.Clear; UniQuery1.SQL.Text:='INSERT INTO User (Username,password) VALUES (: Username,:p assword)'; UniQuery1.Params.ParamByName ('username'). Value: ='LJ'; UniQuery1.Params.ParamByName ('Password'). IsNull;//The field is set to NOT NULL, so there is an error hereUniquery1.execsql;//when the transaction is turned on, the above three bars will be rolled backunitransaction1.connections[0]. Commit; exceptOn E:exception Do beginunitransaction1.connections[0]. Rollback; ShowMessage (E.message); End; End; finallyUniconnection1.disconnect; End;End;
MySQL + UNIDAC Use transaction example
MySQL + UNIDAC Use transaction example