1. 打开事务通过Connetion.BeginTransaction()
2. 通过设置Command 的Command.Transaction 属性,在事务中加入语句或存储过程.
3. 依靠提供者,选择使用 Transaction.Save() 或 Transaction.Begin() 来创建savepoint 或内嵌的事务来使能局部回滚.
4. 提交或回滚事务通过使用Transaction.Commit() 或Transaction.Rollback()
所有的数据提供者提供了同样的基本结构来处理事务,开始连接的事务,加入指令,使用事务物件来提交或回滚.
Sql Server 事务
Sql Server 数据提供对象提供事务的基本功能情况见以下的类和成员:
Class |
Member |
Function |
SqlConnection |
BeginTransaction |
Open a new transaction. |
SqlCommand |
Transaction |
Enlist the command associated with the object in an open transaction. |
SqlTransaction |
Save |
Create a savepoint to enable a partial rollback. |
SqlTransaction |
Rollback |
Roll back a transaction. |
SqlTransaction |
Commit |
Commit a Transaction |
Sql Server 数据提供者( 对于Sql Server 来说,OLE DB provider 也是如此) 不提供在代码中创建内嵌事务的方式, 因此内嵌事务在Sql Server并不真正的被支持.因此这儿只有一种方式来开始一个事务在SqlClient中-使用SqlConnection.BeginTransaction(). 你不能在统一个连接中打开另外一个事务,直到先前的事务被提交或回滚,因此在任何时间每个连接只有一个打开的事务.为了使用局部回滚,使用SqlTransaction.Save().
OLE DB 事务
OLE DB 数据提供者提供事务的基本功能情况见以下的类和成员:
Class |
Member |
Function |
OleDbConnection |
BeginTransaction |
Open a new transaction. |
OleDbCommand |
Transaction |
Enlist the command associated with the object in an open transaction. |
OleDbTransaction |
Begin |
Create a nested transaction, to enable a partial rollback. |
OleDbTransaction |
Rollback |
Roll back a transaction. |
OleDbTransaction |
Commit |
Commit a Transaction |
OLE DB 数据提供者不提供任何的方法来设置savepoints, 相反的有个Begin() 方法在OleDbTransaction物件上.这允许你创建内嵌事务(根据你的数据库)来提供类似的功能.在这种情况下你可以在单个连接中拥有多个事务.但必须记住的是显示的调用提交和回滚.
记住,.NET OLE DB数据提供者的内嵌事务依赖于你的数据库和指定的OLE DB提供者.为了成功实施内嵌事务,其必须被你的数据库和OLE DB 提供者都支持.
Oracle 事务
Oracle 数据提供者提供事务的基本功能情况见以下的类和成员:
Class |
Member |
Function |
OracleConnection |
BeginTransaction |
Open a new transaction. |
OracleCommand |
Transaction |
Enlist the command associated with the object in an open transaction. |
OracleTransaction |
Rollback |
Roll back a transaction. |
OracleTransaction |
Commit |
Commit a Transaction |
Oracle 数据提供者不允许你指定savepoints或内嵌事务.Savepoints 被Oracle 支持,但是你不能实施它们对于当前的oracle数据提供者版本来说.事务于oracle 来说或者回滚或者提交,局部回滚是不能的.