SqlTransaction.Rollback 方法

定义

从挂起状态回滚事务。

重载

Rollback()

从挂起状态回滚事务。

Rollback(String)

从挂起状态回滚事务,并指定事务或保存点名称。

Rollback()

Source:
System.Data.SqlClient.notsupported.cs

从挂起状态回滚事务。

C#
public override void Rollback();
C#
public void Rollback();

实现

例外

在尝试提交事务时出错。

已提交或回滚事务。

连接已断开。

示例

以下示例创建 SqlConnectionSqlTransaction。 它还演示如何使用 BeginTransactionCommitRollback 方法。 事务在任何错误时都回滚。 Try / Catch 错误处理用于在尝试提交或回滚事务时处理任何错误。

C#
private static void ExecuteSqlTransaction(string connectionString)
{
    using (SqlConnection connection = new SqlConnection(connectionString))
    {
        connection.Open();

        SqlCommand command = connection.CreateCommand();
        SqlTransaction transaction;

        // Start a local transaction.
        transaction = connection.BeginTransaction();

        // Must assign both transaction object and connection
        // to Command object for a pending local transaction
        command.Connection = connection;
        command.Transaction = transaction;

        try
        {
            command.CommandText =
                "Insert into Region (RegionID, RegionDescription) VALUES (100, 'Description')";
            command.ExecuteNonQuery();
            command.CommandText =
                "Insert into Region (RegionID, RegionDescription) VALUES (101, 'Description')";
            command.ExecuteNonQuery();

            // Attempt to commit the transaction.
            transaction.Commit();
            Console.WriteLine("Both records are written to database.");
        }
        catch (Exception ex)
        {
            Console.WriteLine("Commit Exception Type: {0}", ex.GetType());
            Console.WriteLine("  Message: {0}", ex.Message);

            // Attempt to roll back the transaction.
            try
            {
                transaction.Rollback();
            }
            catch (Exception ex2)
            {
                // This catch block will handle any errors that may have occurred
                // on the server that would cause the rollback to fail, such as
                // a closed connection.
                Console.WriteLine("Rollback Exception Type: {0}", ex2.GetType());
                Console.WriteLine("  Message: {0}", ex2.Message);
            }
        }
    }
}

注解

方法 Rollback 等效于 Transact-SQL ROLLBACK TRANSACTION 语句。 有关详细信息,请参阅 ROLLBACK TRANSACTION (Transact-SQL)

事务只能在调用 Commit) 之前BeginTransaction从挂起状态 (回滚。 如果事务在之前释放 CommitRollback 被调用,则会回滚该事务。

备注

Try / Catch 回滚事务时,应始终使用异常处理。 Rollback如果连接终止或事务已在服务器上回滚,则生成 InvalidOperationException

有关SQL Server事务的详细信息,请参阅事务 (Transact-SQL)

另请参阅

适用于

.NET 10 (package-provided) 和其他版本
产品 版本
.NET Core 1.0, Core 1.1, 6 (package-provided), 7 (package-provided), 8 (package-provided), 9 (package-provided), 10 (package-provided)
.NET Framework 1.1, 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 (package-provided)

Rollback(String)

Source:
System.Data.SqlClient.notsupported.cs

从挂起状态回滚事务,并指定事务或保存点名称。

C#
public void Rollback(string transactionName);

参数

transactionName
String

要回滚的事务的名称,或要回滚到的保存点的名称。

例外

未指定事务名称。

已提交或回滚事务。

连接已断开。

示例

以下示例创建 SqlConnectionSqlTransaction。 它还演示如何使用 BeginTransactionCommitRollback 方法。 事务在任何错误时都回滚。 Try / Catch 错误处理用于在尝试提交或回滚事务时处理任何错误。

C#
private static void ExecuteSqlTransaction(string connectionString)
{
    using (SqlConnection connection = new SqlConnection(connectionString))
    {
        connection.Open();

        SqlCommand command = connection.CreateCommand();
        SqlTransaction transaction;

        // Start a local transaction.
        transaction = connection.BeginTransaction("SampleTransaction");

        // Must assign both transaction object and connection
        // to Command object for a pending local transaction
        command.Connection = connection;
        command.Transaction = transaction;

        try
        {
            command.CommandText =
                "Insert into Region (RegionID, RegionDescription) VALUES (100, 'Description')";
            command.ExecuteNonQuery();
            command.CommandText =
                "Insert into Region (RegionID, RegionDescription) VALUES (101, 'Description')";
            command.ExecuteNonQuery();

            // Attempt to commit the transaction.
            transaction.Commit();
            Console.WriteLine("Both records are written to database.");
        }
        catch (Exception ex)
        {
            Console.WriteLine("Commit Exception Type: {0}", ex.GetType());
            Console.WriteLine("  Message: {0}", ex.Message);

            // Attempt to roll back the transaction.
            try
            {
                transaction.Rollback("SampleTransaction");
            }
            catch (Exception ex2)
            {
                // This catch block will handle any errors that may have occurred
                // on the server that would cause the rollback to fail, such as
                // a closed connection.
                Console.WriteLine("Rollback Exception Type: {0}", ex2.GetType());
                Console.WriteLine("  Message: {0}", ex2.Message);
            }
        }
    }
}

注解

方法 Rollback 等效于 Transact-SQL ROLLBACK TRANSACTION 语句。 有关详细信息,请参阅 事务 (Transact-SQL)

事务只能在调用 Commit) 之前BeginTransaction从挂起状态 (回滚。 如果事务在或Rollback调用之前Commit释放,则会回滚该事务。

备注

Try / Catch 回滚事务时,应始终使用异常处理。 Rollback如果连接终止或事务已在服务器上回滚,则生成 InvalidOperationException

有关SQL Server事务的详细信息,请参阅事务 (Transact-SQL)

另请参阅

适用于

.NET 10 (package-provided) 和其他版本
产品 版本
.NET Core 1.0, Core 1.1, 6 (package-provided), 7 (package-provided), 8 (package-provided), 9 (package-provided), 10 (package-provided)
.NET Framework 1.1, 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 (package-provided)