SqlTransaction.Rollback 메서드
정의
중요
일부 정보는 릴리스되기 전에 상당 부분 수정될 수 있는 시험판 제품과 관련이 있습니다. Microsoft는 여기에 제공된 정보에 대해 어떠한 명시적이거나 묵시적인 보증도 하지 않습니다.
보류 중인 상태에서 트랜잭션을 롤백합니다.
오버로드
| Name | Description |
|---|---|
| Rollback() |
보류 중인 상태에서 트랜잭션을 롤백합니다. |
| Rollback(String) |
보류 중인 상태에서 트랜잭션을 롤백하고 트랜잭션 또는 저장점 이름을 지정합니다. |
Rollback()
보류 중인 상태에서 트랜잭션을 롤백합니다.
public:
virtual void Rollback();
public:
override void Rollback();
public void Rollback();
public override void Rollback();
abstract member Rollback : unit -> unit
override this.Rollback : unit -> unit
override this.Rollback : unit -> unit
Public Sub Rollback ()
Public Overrides Sub Rollback ()
구현
예외
트랜잭션을 커밋하는 동안 오류가 발생했습니다.
예제
다음 예제에서는 a 및 .를 SqlConnectionSqlTransaction만듭니다. 또한 , Commit및 Rollback 메서드를 BeginTransaction사용하는 방법을 보여 줍니다. 트랜잭션은 오류에 대해 롤백됩니다.
Try
/
Catch 오류 처리는 트랜잭션을 커밋하거나 롤백하려고 할 때 오류를 처리하는 데 사용됩니다.
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);
}
}
}
}
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()
' 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 문과 동일합니다. 자세한 내용은 ROLLBACK TRANSACTION(Transact-SQL) 참조하세요.
트랜잭션은 보류 중인 상태에서만 롤백할 수 있습니다(호출된 후에 BeginTransaction 는 호출되기 전 Commit ). 트랜잭션은 이전에 삭제되거나 Rollback 호출된 Commit 경우에 롤백됩니다.
비고
Try
/
Catch 예외 처리는 트랜잭션을 롤백할 때 항상 사용해야 합니다. A Rollback 는 InvalidOperationException 연결이 종료되거나 트랜잭션이 이미 서버에서 롤백된 경우를 생성합니다.
SQL Server 트랜잭션에 대한 자세한 내용은 전송(Transact-SQL) 참조하세요.
추가 정보
적용 대상
Rollback(String)
보류 중인 상태에서 트랜잭션을 롤백하고 트랜잭션 또는 저장점 이름을 지정합니다.
public:
void Rollback(System::String ^ transactionName);
public void Rollback(string transactionName);
member this.Rollback : string -> unit
override this.Rollback : string -> unit
Public Sub Rollback (transactionName As String)
매개 변수
- transactionName
- String
롤백할 트랜잭션의 이름 또는 롤백할 저장점입니다.
예외
트랜잭션 이름이 지정되지 않았습니다.
예제
다음 예제에서는 a 및 .를 SqlConnectionSqlTransaction만듭니다. 또한 , Commit및 Rollback 메서드를 BeginTransaction사용하는 방법을 보여 줍니다. 트랜잭션은 오류에 대해 롤백됩니다.
Try
/
Catch 오류 처리는 트랜잭션을 커밋하거나 롤백하려고 할 때 오류를 처리하는 데 사용됩니다.
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);
}
}
}
}
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("SampleTransaction")
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 문과 동일합니다. 자세한 내용은 전송(Transact-SQL) 참조하세요.
트랜잭션은 보류 중인 상태에서만 롤백할 수 있습니다(호출된 후에 BeginTransaction 는 호출되기 전 Commit ). 트랜잭션은 이전에 CommitRollback 삭제되거나 호출된 경우 롤백됩니다.
비고
Try
/
Catch 예외 처리는 트랜잭션을 롤백할 때 항상 사용해야 합니다. A Rollback 는 InvalidOperationException 연결이 종료되거나 트랜잭션이 이미 서버에서 롤백된 경우를 생성합니다.
SQL Server 트랜잭션에 대한 자세한 내용은 전송(Transact-SQL) 참조하세요.