[ Team LiB ] |
31.3 Methods Reference
Starts a nested database transaction. Parameters
ExampleThis 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(); NotesThis method is available only in the OLE DB .NET data provider. An InvalidOperationException is raised if the data source doesn't support nested transactions.
Commits the database transaction. ParametersNone. ExampleThe 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(); }
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. ParametersNone. ExampleSee the Example for the Commit( ) method in this chapter. NoteThe overloaded Rollback( ) method with the savepoint name argument is available only in the SQL Server .NET data provider.
Creates a savepoint in the transaction that can roll back a portion of the transaction. ParametersNone. ExampleThe 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(); } NoteThis method is available only in the SQL Server .NET data provider. |
[ Team LiB ] |