SqlTransaction クラス
定義
重要
一部の情報は、リリース前に大きく変更される可能性があるプレリリースされた製品に関するものです。 Microsoft は、ここに記載されている情報について、明示または黙示を問わず、一切保証しません。
SQL Server データベースで実行されるTransact-SQL トランザクションを表します。 このクラスは継承できません。
public ref class SqlTransaction sealed : MarshalByRefObject, IDisposable, System::Data::IDbTransaction
public ref class SqlTransaction sealed : System::Data::Common::DbTransaction
public sealed class SqlTransaction : MarshalByRefObject, IDisposable, System.Data.IDbTransaction
public sealed class SqlTransaction : System.Data.Common.DbTransaction
type SqlTransaction = class
inherit MarshalByRefObject
interface IDbTransaction
interface IDisposable
type SqlTransaction = class
inherit DbTransaction
Public NotInheritable Class SqlTransaction
Inherits MarshalByRefObject
Implements IDbTransaction, IDisposable
Public NotInheritable Class SqlTransaction
Inherits DbTransaction
- 継承
- 継承
- 実装
例
次の例では、 SqlConnection と SqlTransactionを作成します。 また、 BeginTransaction、 Commit、および Rollback メソッドの使用方法についても説明します。 トランザクションは、エラーに対してロールバックされるか、最初にコミットされずに破棄された場合にロールバックされます。
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
注釈
アプリケーションは、SqlConnection オブジェクトのBeginTransactionを呼び出して、SqlTransaction オブジェクトを作成します。 トランザクションに関連付けられている後続のすべての操作 (トランザクションのコミットや中止など) は、 SqlTransaction オブジェクトに対して実行されます。
Note
Try
/
Catch 例外処理は、 SqlTransactionをコミットまたはロールバックするときに常に使用する必要があります。 接続が終了した場合、またはトランザクションが既にサーバーにロールバックされている場合は、 Commit と Rollback の両方で InvalidOperationException が生成されます。
SQL Server トランザクションの詳細については、「Explicit Transactions」および「Coding Efficient Transactions」を参照してください。
プロパティ
| 名前 | 説明 |
|---|---|
| Connection |
トランザクションに関連付けられている SqlConnection オブジェクトを取得するか、トランザクションが無効になった場合に |
| DbConnection |
派生クラスでオーバーライドされると、トランザクションに関連付けられている DbConnection オブジェクトを取得します。 (継承元 DbTransaction) |
| IsolationLevel |
このトランザクションの IsolationLevel を指定します。 |
メソッド
| 名前 | 説明 |
|---|---|
| Commit() |
データベース トランザクションをコミットします。 |
| CreateObjRef(Type) |
リモート オブジェクトとの通信に使用されるプロキシの生成に必要なすべての関連情報を含むオブジェクトを作成します。 (継承元 MarshalByRefObject) |
| Dispose() |
オブジェクトによって保持されているリソースを解放します。 |
| Dispose() |
DbTransactionによって使用されるアンマネージ リソースを解放します。 (継承元 DbTransaction) |
| Dispose(Boolean) |
DbTransactionによって使用されるアンマネージ リソースを解放し、必要に応じてマネージド リソースを解放します。 (継承元 DbTransaction) |
| Equals(Object) |
指定したオブジェクトが現在のオブジェクトと等しいかどうかを判断します。 (継承元 Object) |
| GetHashCode() |
既定のハッシュ関数として機能します。 (継承元 Object) |
| GetLifetimeService() |
このインスタンスの有効期間ポリシーを制御する現在の有効期間サービス オブジェクトを取得します。 (継承元 MarshalByRefObject) |
| GetType() |
現在のインスタンスの Type を取得します。 (継承元 Object) |
| InitializeLifetimeService() |
このインスタンスの有効期間ポリシーを制御する有効期間サービス オブジェクトを取得します。 (継承元 MarshalByRefObject) |
| MemberwiseClone() |
現在の Objectの簡易コピーを作成します。 (継承元 Object) |
| MemberwiseClone(Boolean) |
現在の MarshalByRefObject オブジェクトの簡易コピーを作成します。 (継承元 MarshalByRefObject) |
| Rollback() |
保留中の状態からトランザクションをロールバックします。 |
| Rollback(String) |
保留中の状態からトランザクションをロールバックし、トランザクションまたはセーブポイント名を指定します。 |
| Save(String) |
トランザクションの一部をロールバックするために使用できるセーブポイントをトランザクションに作成し、セーブポイント名を指定します。 |
| ToString() |
現在のオブジェクトを表す文字列を返します。 (継承元 Object) |
明示的なインターフェイスの実装
| 名前 | 説明 |
|---|---|
| IDbTransaction.Connection |
トランザクションに関連付けられている DbConnection オブジェクトを取得します。トランザクションが無効になった場合は null 参照を取得します。 (継承元 DbTransaction) |