'Declaration Public Class SqlTransaction Inherits Devart.Common.DbTransactionBase Implements System.Data.IDbTransaction, System.IDisposable
public class SqlTransaction : Devart.Common.DbTransactionBase, System.Data.IDbTransaction, System.IDisposable
'Declaration Public Class SqlTransaction Inherits Devart.Common.DbTransactionBase Implements System.Data.IDbTransaction, System.IDisposable
public class SqlTransaction : Devart.Common.DbTransactionBase, System.Data.IDbTransaction, System.IDisposable
The application creates a SqlTransaction object by calling SqlConnection.BeginTransaction on the SqlConnection object. All subsequent operations associated with the transaction (for example, committing or aborting the transaction), are performed on the SqlTransaction object.
public static void RunSqlTransaction(string myConnString) { using (SqlConnection myConnection = new SqlConnection(myConnString)) { myConnection.Open(); // Start a local transaction SqlTransaction myTrans = myConnection.BeginTransaction(System.Data.IsolationLevel.ReadCommitted); SqlCommand myCommand = myConnection.CreateCommand(); try { myCommand.CommandText = "INSERT INTO Dept(DeptNo, DName) Values(52, 'DEVELOPMENT')"; myCommand.ExecuteNonQuery(); myCommand.CommandText = "INSERT INTO Dept(DeptNo, DName) Values(62, 'PRODUCTION')"; myCommand.ExecuteNonQuery(); myTrans.Commit(); Console.WriteLine("Both records are written to database."); } catch (Exception e) { myTrans.Rollback(); Console.WriteLine(e.ToString()); Console.WriteLine("Neither record was written to database."); } finally { myCommand.Dispose(); myTrans.Dispose(); } } }
Public Shared Sub RunSqlTransaction(myConnString As String) Using myConnection As New SqlConnection(myConnString) myConnection.Open() ' Start a local transaction Dim myTrans As SqlTransaction = myConnection.BeginTransaction(System.Data.IsolationLevel.ReadCommitted) Dim myCommand As SqlCommand = myConnection.CreateCommand() Try myCommand.CommandText = "INSERT INTO Dept(DeptNo, DName) Values(52, 'DEVELOPMENT')" myCommand.ExecuteNonQuery() myCommand.CommandText = "INSERT INTO Dept(DeptNo, DName) Values(62, 'PRODUCTION')" myCommand.ExecuteNonQuery() myTrans.Commit() Console.WriteLine("Both records are written to database.") Catch e As Exception myTrans.Rollback() Console.WriteLine(e.ToString()) Console.WriteLine("Neither record was written to database.") Finally myCommand.Dispose() myTrans.Dispose() End Try End Using End Sub
System.Object
System.MarshalByRefObject
System.Data.Common.DbTransaction
Devart.Common.DbTransactionBase
Devart.Data.SqlServer.SqlTransaction
Target Platforms: Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2