SqlTransaction.Rollback 方法
定义
从挂起状态回滚事务。Rolls back a transaction from a pending state.
重载
| Rollback() |
从挂起状态回滚事务。Rolls back a transaction from a pending state. |
| Rollback(String) |
从挂起状态回滚事务,并指定事务或保存点名称。Rolls back a transaction from a pending state, and specifies the transaction or savepoint name. |
Rollback()
从挂起状态回滚事务。Rolls back a transaction from a pending state.
public:
override void Rollback();
public:
virtual void Rollback();
public override void Rollback ();
public void Rollback ();
override this.Rollback : unit -> unit
abstract member Rollback : unit -> unit
override this.Rollback : unit -> unit
Public Overrides Sub Rollback ()
Public Sub Rollback ()
实现
例外
在尝试提交事务时出错。An error occurred while trying to commit the transaction.
已提交或回滚事务。The transaction has already been committed or rolled back.
或-or-
连接已断开。The connection is broken.
示例
下面的示例创建一个 SqlConnection 和一个 SqlTransaction 。The following example creates a SqlConnection and a SqlTransaction. 它还演示了如何使用 BeginTransaction 、 Commit 和 Rollback 方法。It also demonstrates how to use the BeginTransaction, Commit, and Rollback methods. 任何错误都将回滚事务。The transaction is rolled back on any error. Try/Catch 错误处理用于在尝试提交或回滚事务时处理任何错误。Try/Catch error handling is used to handle any errors when attempting to commit or roll back the transaction.
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();
}
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);
}
}
}
}
Private Sub ExecuteSqlTransaction(ByVal connectionString As String)
Using connection As New SqlConnection(connectionString)
connection.Open()
Dim command As SqlCommand = connection.CreateCommand()
Dim transaction As SqlTransaction
' 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 ex As Exception
Console.WriteLine("Commit Exception Type: {0}", ex.GetType())
Console.WriteLine(" Message: {0}", ex.Message)
' Attempt to roll back the transaction.
Try
transaction.Rollback()
Catch ex2 As Exception
' 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)
End Try
End Try
End Using
End Sub
注解
Rollback方法等效于 TRANSACT-SQL ROLLBACK TRANSACTION 语句。The Rollback method is equivalent to the Transact-SQL ROLLBACK TRANSACTION statement. 有关详细信息,请参阅 ROLLBACK TRANSACTION (transact-sql) 。For more information, see ROLLBACK TRANSACTION (Transact-SQL) .
仅在调用之后但在) 之前,事务才能从挂起状态回滚 (BeginTransaction Commit 。The transaction can only be rolled back from a pending state (after BeginTransaction has been called, but before Commit is called). 当事务在被调用之前被释放时,将回滚该事务 Commit Rollback 。The transaction is rolled back in the event it is disposed before Commit or Rollback is called.
备注
Try/Catch 在回滚事务时,应始终使用异常处理。Try/Catch exception handling should always be used when rolling back a transaction. Rollback InvalidOperationException 如果连接已终止,或者事务已在服务器上回滚,则会生成。A Rollback generates an InvalidOperationException if the connection is terminated or if the transaction has already been rolled back on the server.
有关 SQL Server 事务的详细信息,请参阅 事务 (transact-sql) 。For more information on SQL Server transactions, see Transactions (Transact-SQL).
适用于
Rollback(String)
从挂起状态回滚事务,并指定事务或保存点名称。Rolls back a transaction from a pending state, and specifies the transaction or savepoint name.
public:
void Rollback(System::String ^ transactionName);
public void Rollback (string transactionName);
override this.Rollback : string -> unit
member this.Rollback : string -> unit
Public Sub Rollback (transactionName As String)
参数
- transactionName
- String
要回滚的事务的名称,或要回滚到的保存点的名称。The name of the transaction to roll back, or the savepoint to which to roll back.
例外
未指定事务名称。No transaction name was specified.
已提交或回滚事务。The transaction has already been committed or rolled back.
或-or-
连接已断开。The connection is broken.
示例
下面的示例创建一个 SqlConnection 和一个 SqlTransaction 。The following example creates a SqlConnection and a SqlTransaction. 它还演示了如何使用 BeginTransaction 、 Commit 和 Rollback 方法。It also demonstrates how to use the BeginTransaction, Commit, and Rollback methods. 任何错误都将回滚事务。The transaction is rolled back on any error. Try/Catch 错误处理用于在尝试提交或回滚事务时处理任何错误。Try/Catch error handling is used to handle any errors when attempting to commit or roll back the transaction.
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();
}
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);
}
}
}
}
Private Sub ExecuteSqlTransaction(ByVal connectionString As String)
Using connection As New SqlConnection(connectionString)
connection.Open()
Dim command As SqlCommand = connection.CreateCommand()
Dim transaction As SqlTransaction
' 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 ex As Exception
Console.WriteLine("Exception Type: {0}", ex.GetType())
Console.WriteLine(" Message: {0}", ex.Message)
' Attempt to roll back the transaction.
Try
transaction.Rollback()
Catch ex2 As Exception
' 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)
End Try
End Try
End Using
End Sub
注解
Rollback方法等效于 TRANSACT-SQL ROLLBACK TRANSACTION 语句。The Rollback method is equivalent to the Transact-SQL ROLLBACK TRANSACTION statement. 有关详细信息,请参阅 事务 (transact-sql) 。For more information, see Transactions (Transact-SQL).
仅在调用之后但在) 之前,事务才能从挂起状态回滚 (BeginTransaction Commit 。The transaction can only be rolled back from a pending state (after BeginTransaction has been called, but before Commit is called). 如果事务在调用之前被释放,则回滚 Commit 事务 Rollback 。The transaction is rolled back if it is disposed before Commit or Rollback is called.
备注
Try/Catch 在回滚事务时,应始终使用异常处理。Try/Catch exception handling should always be used when rolling back a transaction. Rollback InvalidOperationException 如果连接已终止,或者事务已在服务器上回滚,则会生成。A Rollback generates an InvalidOperationException if the connection is terminated or if the transaction has already been rolled back on the server.
有关 SQL Server 事务的详细信息,请参阅 事务 (transact-sql) 。For more information on SQL Server transactions, see Transactions (Transact-SQL).