dotConnect for MySQL Documentation
Devart.Data.MySql Namespace / MySqlTransaction Class
Members Example

In This Topic
    MySqlTransaction Class
    In This Topic
    Represents a SQL transaction to be made in the MySQL database.
    Syntax
    'Declaration
     
    Public Class MySqlTransaction 
       Inherits Devart.Common.DbTransactionBase
       Implements System.Data.IDbTransactionSystem.IDisposable 
    Remarks

    The application creates a MySqlTransaction object by calling MySqlConnection.BeginTransaction on the MySqlConnection object. All subsequent operations associated with the transaction (for example, committing or aborting the transaction), are performed on the MySqlTransaction object.

    The correlation between MySqlConnection and MySqlTransaction is always 1:1.

    Transactions (local and global) are possible only with tables that are created with engine supporting transactions. In standard MySQL distribution, InnoDB engine supports transactions.

    Example
    The following example creates a MySqlConnection and a MySqlTransaction. It also demonstrates how to use the MySqlConnection.BeginTransaction, Commit, and Rollback methods.
    public static void RunMySqlTransaction(string myConnString) {
            using (MySqlConnection myConnection = new MySqlConnection(myConnString)) {
                    myConnection.Open();
                    // Start a local transaction
                    MySqlTransaction myTrans = myConnection.BeginTransaction(System.Data.IsolationLevel.ReadCommitted);
                    MySqlCommand 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 RunMySqlTransaction(myConnString As String)
            Using myConnection As New MySqlConnection(myConnString)
                    myConnection.Open()
                    ' Start a local transaction
                    Dim myTrans As MySqlTransaction = myConnection.BeginTransaction(System.Data.IsolationLevel.ReadCommitted)
                    Dim myCommand As MySqlCommand = 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
    Inheritance Hierarchy

    System.Object
       System.MarshalByRefObject
          System.Data.Common.DbTransaction
             Devart.Common.DbTransactionBase
                Devart.Data.MySql.MySqlTransaction

    Requirements

    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

    See Also