Class MySqlTransaction
Represents a SQL transaction to be made in a MySQL database. This class cannot be inherited.
Inheritance
Implements
Namespace: MySql.Data.MySqlClient
Assembly: MySql.Data.dll
Version: 9.3.0
Syntax
public sealed class MySqlTransaction : DbTransaction, IDbTransaction, IDisposable, IAsyncDisposable
Remarks
The application creates a My
Examples
The following example creates a My
public void RunTransaction(string myConnString)
{
MySqlConnection myConnection = new MySqlConnection(myConnString);
myConnection.Open();
MySqlCommand myCommand = myConnection.CreateCommand();
MySqlTransaction myTrans;
// Start a local transaction
myTrans = myConnection.BeginTransaction();
// Must assign both transaction object and connection
// to Command object for a pending local transaction
myCommand.Connection = myConnection;
myCommand.Transaction = myTrans;
try
{
myCommand.CommandText = "Insert into Region (RegionID, RegionDescription) VALUES (100, 'Description')";
myCommand.ExecuteNonQuery();
myCommand.CommandText = "Insert into Region (RegionID, RegionDescription) VALUES (101, 'Description')";
myCommand.ExecuteNonQuery();
myTrans.Commit();
Console.WriteLine("Both records are written to database.");
}
catch(Exception e)
{
try
{
myTrans.Rollback();
}
catch (MySqlException ex)
{
if (myTrans.Connection != null)
{
Console.WriteLine("An exception of type " + ex.GetType() +
" was encountered while attempting to roll back the transaction.");
}
}
Console.WriteLine("An exception of type " + e.GetType() +
" was encountered while inserting the data.");
Console.WriteLine("Neither record was written to database.");
}
finally
{
myConnection.Close();
}
}
Properties
Connection
Gets the My
Declaration
public MySqlConnection Connection { get; set; }
Property Value
Type | Description |
---|---|
My |
The My |
Remarks
A single application may have multiple database connections, each
with zero or more transactions. This property enables you to
determine the connection object associated with a particular
transaction created by Begin
DbConnection
Gets the Db
Declaration
protected override DbConnection DbConnection { get; }
Property Value
Type | Description |
---|---|
System. |
Overrides
IsolationLevel
Specifies the Isolation
Declaration
public override IsolationLevel IsolationLevel { get; }
Property Value
Type | Description |
---|---|
System. |
The Isolation |
Overrides
Remarks
Parallel transactions are not supported. Therefore, the IsolationLevel applies to the entire transaction.
Methods
Commit()
Commits the database transaction.
Declaration
public override void Commit()
Overrides
Remarks
The Commit() method is equivalent to the MySQL SQL statement COMMIT.
CommitAsync(CancellationToken)
Asynchronously commits the database transaction.
Declaration
public override Task CommitAsync(CancellationToken cancellationToken = default(CancellationToken))
Parameters
Type | Name | Description |
---|---|---|
System. |
cancellationToken |
Returns
Type | Description |
---|---|
System. |
A task representing the asynchronous operation. |
Overrides
Dispose(Boolean)
Releases the unmanaged resources used by the My
Declaration
protected override void Dispose(bool disposing)
Parameters
Type | Name | Description |
---|---|---|
System. |
disposing | If true, this method releases all resources held by any managed objects that
this My |
Overrides
Finalize()
Declaration
protected void Finalize()
Rollback()
Rolls back a transaction from a pending state.
Declaration
public override void Rollback()
Overrides
Remarks
The Rollback() method is equivalent to the MySQL statement ROLLBACK. The transaction can only be rolled back from a pending state (after BeginTransaction has been called, but before Commit is called).
RollbackAsync(CancellationToken)
Asynchronously rolls back a transaction from a pending state.
Declaration
public override Task RollbackAsync(CancellationToken cancellationToken = default(CancellationToken))
Parameters
Type | Name | Description |
---|---|---|
System. |
cancellationToken | The cancellation token. |
Returns
Type | Description |
---|---|
System. |
A task representing the asynchronous operation. |