var Level: integer; begin Level:=ADOConnection1.BeginTrans; try //do some database //updating, deleting or inserting ADOConnection1.CommitTrans; except on E:Exception do ADOConnection1.RollbackTrans; end;//try end;
说明: BeginTrans, CommitTrans, RollBackTrans Database transactions are a means to allow a user to do many operations on a recordset or not to do any of them. There is no such thing in a transaction that one task is done and other is not. Transactions are always executed as a *whole*. By using transactions, you ensure that the database is not left in an inconsistent state when a problem occurs completing one of the actions that make up the transaction. In Delphi ADO's transaction processing, 3 methods are used with the TADOConnection object to save or cancel changes made to the data source. Once you call the BeginTrans method, the provider will no longer instantaneously commit any changes you make until you call CommitTrans or RollbackTrans to end the transaction. Transaction Level The IsolationLevel property is the level of transaction isolation for a TADOConnection object. The purpose of the isolation level is to define how other transactions can interact with your transactions, when they work with the same tables. For example, can you see changes in other transactions before or after they are committed? This property only goes into effect after you make a BeginTrans method call. Transaction processing To start a transaction call the BeginTrans method of the TADOConnection object. BeginTrans returns the nesting level of the new transaction. A return value of "1" indicates you have opened a top-level transaction (that is, the transaction is not nested within another transaction), "2" indicates that you have opened a second-level transaction (a transaction nested within a top-level transaction), and so forth. Once the BeginTrans is executed, the OnBeginTransComplete event is triggered and the InTransaction property to True. Note: Since transactions can be nested, all lower-level transactions must be resolved before you can resolve higher-level transactions. Once you have started a transaction, a call to
try except来捕获异常,有异常则rollback
try except来捕获异常,有异常则rollback var Level: integer; begin Level:=ADOConnection1.BeginTrans; try //do some database //updating, deleting or inserting ADOConnection1.CommitTrans; except on E:Exception do ADOConnection1.RollbackTrans; end;//try end;
DateBase1.BeginTran
.
.
.DateBase1.rollback;
begin
Level:=ADOConnection1.BeginTrans;
try
//do some database
//updating, deleting or inserting
ADOConnection1.CommitTrans;
except
on E:Exception do ADOConnection1.RollbackTrans;
end;//try
end;
BeginTrans, CommitTrans, RollBackTrans
Database transactions are a means to allow a user to do many operations on a recordset or not to do any of them. There is no such thing in a transaction that one task is done and other is not. Transactions are always executed as a *whole*. By using transactions, you ensure that the database is not left in an inconsistent state when a problem occurs completing one of the actions that make up the transaction. In Delphi ADO's transaction processing, 3 methods are used with the TADOConnection object to save or cancel changes made to the data source. Once you call the BeginTrans method, the provider will no longer instantaneously commit any changes you make until you call CommitTrans or RollbackTrans to end the transaction. Transaction Level
The IsolationLevel property is the level of transaction isolation for a TADOConnection object. The purpose of the isolation level is to define how other transactions can interact with your transactions, when they work with the same tables. For example, can you see changes in other transactions before or after they are committed? This property only goes into effect after you make a BeginTrans method call. Transaction processing
To start a transaction call the BeginTrans method of the TADOConnection object. BeginTrans returns the nesting level of the new transaction. A return value of "1" indicates you have opened a top-level transaction (that is, the transaction is not nested within another transaction), "2" indicates that you have opened a second-level transaction (a transaction nested within a top-level transaction), and so forth. Once the BeginTrans is executed, the OnBeginTransComplete event is triggered and the InTransaction property to True. Note: Since transactions can be nested, all lower-level transactions must be resolved before you can resolve higher-level transactions. Once you have started a transaction, a call to
var Level: integer;
begin
Level:=ADOConnection1.BeginTrans;
try
//do some database
//updating, deleting or inserting
ADOConnection1.CommitTrans;
except
on E:Exception do ADOConnection1.RollbackTrans;
end;//try
end;