Best practice to work with SQL Server transactions
Hello,
what's the best way to perform a transaction with a TMSQuery?
See the following source code:
try
LQuery := LDatenHandler.GetQuery; // returns an TMSQuery object
try
LQuery.SQL.Clear;
LQuery.SQL.Add('Begin transaction');
LQuery.SQL.Add('INSERT INTO tbl_test(col1, col2, col3) VALUES (:col1, :col2, :col3);');
LQuery.ParamByName('col1').AsInteger := 1;
LQuery.ParamByName('col2').AsDateTime := Now();
LQuery.ParamByName('col3').AsString := 'test';
LQuery.SQL.Add('Commit');
LQuery.Execute;
result := true
except
on E: Exception do
begin
result := false;
LQuery.SQL.Add('Rollback');
LQuery.Execute;
showMessage(E.Message);
end;
end;
finally
FreeAndNil(LQuery);
end;