SqlTransaction.Rollback 方法

定义

重载

Rollback()

从挂起状态回滚事务。

Rollback(String)

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

Rollback()

从挂起状态回滚事务。

public:
 override void Rollback();
public override void Rollback ();
override this.Rollback : unit -> unit
Public Overrides Sub Rollback ()

例外

在尝试提交事务时出错。

已提交或回滚事务。

- 或 -

连接已断开。

示例

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

using Microsoft.Data.SqlClient;

namespace Transaction1CS
{
    class Program
    {
        static void Main()
        {
            string connectionString =
                "Persist Security Info=False;Integrated Security=SSPI;database=Northwind;server=(local)";
            ExecuteSqlTransaction(connectionString);
            Console.ReadLine();
        }
        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)

事务只能从挂起状态回滚, (调用 之后 BeginTransaction ,但在调用) 之前 Commit 。 事务在被释放之前或Rollback被调用时Commit回滚。

注意

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

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

适用于

Rollback(String)

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

public:
 void Rollback(System::String ^ transactionName);
public:
 override void Rollback(System::String ^ transactionName);
public void Rollback (string transactionName);
public override void Rollback (string transactionName);
override this.Rollback : string -> unit
Public Sub Rollback (transactionName As String)
Public Overrides Sub Rollback (transactionName As String)

参数

transactionName
String

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

例外

未指定事务名称。

已提交或回滚事务。

- 或 -

连接已断开。

示例

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

using Microsoft.Data.SqlClient;

namespace Transaction1CS
{
    class Program
    {
        static void Main()
        {
            string connectionString =
                "Persist Security Info=False;Integrated Security=SSPI;database=Northwind;server=(local)";
            ExecuteSqlTransaction(connectionString);
            Console.ReadLine();
        }
        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)

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

注意

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

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

适用于