Op Englesch liesen Editéieren

Deelen iwwer


CommittableTransaction Constructors

Definition

Initializes a new instance of the CommittableTransaction class.

Overloads

CommittableTransaction()

Initializes a new instance of the CommittableTransaction class.

CommittableTransaction(TimeSpan)

Initializes a new instance of the CommittableTransaction class with the specified timeout value.

CommittableTransaction(TransactionOptions)

Initializes a new instance of the CommittableTransaction class with the specified transaction options.

CommittableTransaction()

Source:
CommittableTransaction.cs
Source:
CommittableTransaction.cs
Source:
CommittableTransaction.cs

Initializes a new instance of the CommittableTransaction class.

C#
public CommittableTransaction();

Examples

The following sample creates a new instance of CommittableTransaction and commits it.

C#
//Create a committable transaction
tx = new CommittableTransaction();

SqlConnection myConnection = new SqlConnection("server=(local)\\SQLExpress;Integrated Security=SSPI;database=northwind");
SqlCommand myCommand = new SqlCommand();

//Open the SQL connection
myConnection.Open();

//Give the transaction to SQL to enlist with
myConnection.EnlistTransaction(tx);

myCommand.Connection = myConnection;

// Restore database to near it's original condition so sample will work correctly.
myCommand.CommandText = "DELETE FROM Region WHERE (RegionID = 100) OR (RegionID = 101)";
myCommand.ExecuteNonQuery();

// Insert the first record.
myCommand.CommandText = "Insert into Region (RegionID, RegionDescription) VALUES (100, 'MidWestern')";
myCommand.ExecuteNonQuery();

// Insert the second record.
myCommand.CommandText = "Insert into Region (RegionID, RegionDescription) VALUES (101, 'MidEastern')";
myCommand.ExecuteNonQuery();

// Commit or rollback the transaction
while (true)
{
    Console.Write("Commit or Rollback? [C|R] ");
    ConsoleKeyInfo c = Console.ReadKey();
    Console.WriteLine();

    if ((c.KeyChar == 'C') || (c.KeyChar == 'c'))
    {
        tx.Commit();
        break;
    }
    else if ((c.KeyChar == 'R') || (c.KeyChar == 'r'))
    {
        tx.Rollback();
        break;
    }
}
myConnection.Close();
tx = null;

Applies to

.NET 10 an aner Versiounen
Produkt Versiounen
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

CommittableTransaction(TimeSpan)

Source:
CommittableTransaction.cs
Source:
CommittableTransaction.cs
Source:
CommittableTransaction.cs

Initializes a new instance of the CommittableTransaction class with the specified timeout value.

C#
public CommittableTransaction(TimeSpan timeout);

Parameters

timeout
TimeSpan

The maximum amount of time the transaction can exist, before it is aborted.

Applies to

.NET 10 an aner Versiounen
Produkt Versiounen
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

CommittableTransaction(TransactionOptions)

Source:
CommittableTransaction.cs
Source:
CommittableTransaction.cs
Source:
CommittableTransaction.cs

Initializes a new instance of the CommittableTransaction class with the specified transaction options.

C#
public CommittableTransaction(System.Transactions.TransactionOptions options);

Parameters

options
TransactionOptions

A TransactionOptions structure that describes the transaction options to use for the new transaction.

Exceptions

options is invalid.

Examples

The following sample creates a new instance of CommittableTransaction with options, and aborts it.

Applies to

.NET 10 an aner Versiounen
Produkt Versiounen
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1