ADO.NET in a Nutshell
Begin [OLE DB only] |
Transaction nestTran = Transaction.Begin(); Transaction nestTran = Transaction.Begin(IsolationLevel il ); |
Starts a nested database transaction.
Parameters
- nestTran
-
Returns a reference to the nested database transaction.
- il
-
The isolation level to use for the new transaction.
Example
This example demonstrates how to start a nested transaction using the Begin method:
String connString = "Data Source=(local);Integrated security=SSPI;" + "Initial Catalog=Northwind;"; OleDbConnection conn = new OleDbConnection(connString); conn.Open(); OleDbTransaction tran = conn.BeginTransaction(); // start a nested transaction OleDbTransaction nestTran = tran.Begin();
Notes
This method is available only in the OLE DB .NET data provider.
An InvalidOperationException is raised if the data source doesn't support nested transactions.
Commit |
Transaction.Commit(); |
Commits the database transaction.
Parameters
None.
Example
The following example demonstrates how to start a transaction and either commit the transaction or roll the transaction back, depending on the outcome of commands executed against the data source:
String connString = "Data Source=(local);Integrated security=SSPI;" + "Initial Catalog=Northwind;"; SqlConnection conn = new SqlConnection(connString); conn.Open(); SqlTransaction tran = conn.BeginTransaction(); try { // ... execute some commands against the data source tran.Commit(); } catch (Exception e) { tran.Rollback(); } finally { conn.Close(); }
Rollback |
Transaction.Rollback(); Transaction.Rollback(String savePointName ); |
Rolls back a transaction from a pending state. A savepoint can optionally be specified as the point to roll the transaction back to with the SQL Server .NET data provider.
Parameters
None.
Example
See the Example for the Commit( ) method in this chapter.
Note
The overloaded Rollback( ) method with the savepoint name argument is available only in the SQL Server .NET data provider.
Save [SQL Server only] |
void = Transaction.Save(String savePointName ); |
Creates a savepoint in the transaction that can roll back a portion of the transaction.
Parameters
None.
Example
The following example demonstrates how to create and use a savepoint to partially roll back a transaction:
String connString = "Data Source=(local);Integrated security=SSPI;" + "Initial Catalog=Northwind;"; SqlConnection conn = new SqlConnection(connString); conn.Open(); SqlTransaction tran = conn.BeginTransaction(); try { // ... execute some commands against the data source } catch (Exception ex) { // roll back the transaction, close the connection, and leave tran.Rollback(); conn.Close(); return; } // create a save point called SavePoint1 tran.Save("SavePoint1"); try { // ... execute some commands against the data source tran.Commit(); } catch (SqlException ex) { // roll back the transaction to the save point tran.Rollback("SavePoint1"); // commit all processing up to the save point tran.Commit(); } finally { conn.Close(); }
Note
This method is available only in the SQL Server .NET data provider.