CommittableTransaction 构造函数

定义

初始化 CommittableTransaction 类的新实例。Initializes a new instance of the CommittableTransaction class.

重载

CommittableTransaction()

初始化 CommittableTransaction 类的新实例。Initializes a new instance of the CommittableTransaction class.

CommittableTransaction(TimeSpan)

使用指定的 timeout 值初始化 CommittableTransaction 类的新实例。Initializes a new instance of the CommittableTransaction class with the specified timeout value.

CommittableTransaction(TransactionOptions)

使用指定的事务选项初始化 CommittableTransaction 类的新实例。Initializes a new instance of the CommittableTransaction class with the specified transaction options.

CommittableTransaction()

初始化 CommittableTransaction 类的新实例。Initializes a new instance of the CommittableTransaction class.

public:
 CommittableTransaction();
public CommittableTransaction ();
Public Sub New ()

例外

尝试在 Windows 98、Windows 98 Second Edition 或 Windows Millennium Edition 下创建事务。An attempt to create a transaction under Windows 98, Windows 98 Second Edition or Windows Millennium Edition.

示例

下面的示例创建 CommittableTransaction 的新实例,并提交该实例。The following sample creates a new instance of CommittableTransaction and commits it.

//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;
tx = New CommittableTransaction

Dim myConnection As New SqlConnection("server=(local)\SQLExpress;Integrated Security=SSPI;database=northwind")
Dim myCommand As 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
Dim c As ConsoleKeyInfo
While (True)
    Console.Write("Commit or Rollback? [C|R] ")
    c = Console.ReadKey()
    Console.WriteLine()

    If (c.KeyChar = "C") Or (c.KeyChar = "c") Then
        tx.Commit()
        Exit While
    ElseIf ((c.KeyChar = "R") Or (c.KeyChar = "r")) Then
        tx.Rollback()
        Exit While
    End If
End While

myConnection.Close()
tx = Nothing

CommittableTransaction(TimeSpan)

使用指定的 timeout 值初始化 CommittableTransaction 类的新实例。Initializes a new instance of the CommittableTransaction class with the specified timeout value.

public:
 CommittableTransaction(TimeSpan timeout);
public CommittableTransaction (TimeSpan timeout);
new System.Transactions.CommittableTransaction : TimeSpan -> System.Transactions.CommittableTransaction
Public Sub New (timeout As TimeSpan)

参数

timeout
TimeSpan

事务在中止之前能够存在的最长时间。The maximum amount of time the transaction can exist, before it is aborted.

例外

尝试在 Windows 98、Windows 98 Second Edition 或 Windows Millennium Edition 下创建事务。An attempt to create a transaction under Windows 98, Windows 98 Second Edition or Windows Millennium Edition.

CommittableTransaction(TransactionOptions)

使用指定的事务选项初始化 CommittableTransaction 类的新实例。Initializes a new instance of the CommittableTransaction class with the specified transaction options.

public:
 CommittableTransaction(System::Transactions::TransactionOptions options);
public CommittableTransaction (System.Transactions.TransactionOptions options);
new System.Transactions.CommittableTransaction : System.Transactions.TransactionOptions -> System.Transactions.CommittableTransaction
Public Sub New (options As TransactionOptions)

参数

options
TransactionOptions

一个 TransactionOptions 结构,描述用于新事务的事务选项。A TransactionOptions structure that describes the transaction options to use for the new transaction.

例外

尝试在 Windows 98、Windows 98 Second Edition 或 Windows Millennium Edition 下创建事务。An attempt to create a transaction under Windows 98, Windows 98 Second Edition or Windows Millennium Edition.

options 无效。options is invalid.

示例

下面的示例使用选项创建 CommittableTransaction 的新实例,并将其中止。The following sample creates a new instance of CommittableTransaction with options, and aborts it.

适用于