Transaction プロパティ
SqlCeCommand を実行するトランザクションを取得または設定します。
名前空間: System.Data.SqlServerCe
アセンブリ: System.Data.SqlServerCe (System.Data.SqlServerCe.dll)
構文
'宣言
Public Property Transaction As SqlCeTransaction
Get
Set
'使用
Dim instance As SqlCeCommand
Dim value As SqlCeTransaction
value = instance.Transaction
instance.Transaction = value
public SqlCeTransaction Transaction { get; set; }
public:
property SqlCeTransaction^ Transaction {
SqlCeTransaction^ get ();
void set (SqlCeTransaction^ value);
}
member Transaction : SqlCeTransaction with get, set
function get Transaction () : SqlCeTransaction
function set Transaction (value : SqlCeTransaction)
プロパティ値
型: System.Data.SqlServerCe. . :: . .SqlCeTransaction
SqlCeTransaction 。既定値は nullNothingnullptrunitNULL 参照 (Visual Basic では Nothing) です。
説明
Transaction プロパティに既に特定の値が設定されていて、コマンドが実行中の場合は、このプロパティは設定できません。SqlCeCommand オブジェクトと同じ SqlCeConnection に接続されていない SqlCeTransaction オブジェクトのトランザクション プロパティを設定すると、次回のステートメント実行時に例外がスローされます。
SqlCeTransaction を null Nothing nullptr unit NULL 参照 (Visual Basic では Nothing) に設定して SqlCeCommand を実行すると、コマンドは自動コミット モードで実行されます。これにより、各ステートメントは、個々のトランザクションで実行されます。
使用例
Dim conn As New SqlCeConnection("Data Source = MyDatabase.sdf; Password ='<pwd>'")
conn.Open()
' Start a local transaction
'
Dim tx As SqlCeTransaction = conn.BeginTransaction()
' By default, commands run in auto-commit mode;
'
Dim cmd1 As SqlCeCommand = conn.CreateCommand()
' You may create multiple commands on the same connection
'
Dim cmd2 As SqlCeCommand = conn.CreateCommand()
' To enlist a command in a transaction, set the Transaction property
'
cmd1.Transaction = tx
Try
cmd1.CommandText = "INSERT INTO Shippers ([Company Name]) VALUES ('Northwind Traders')"
cmd1.ExecuteNonQuery()
' Auto-commited because cmd2 is not enlisted in a transaction
'
cmd2.CommandText = "INSERT INTO Employees ([Last Name], [First Name]) VALUES ('Decker', 'Barbara')"
cmd2.ExecuteNonQuery()
' This will cause referential constraint violation
'
cmd1.CommandText = "DELETE FROM Products WHERE [Product ID] = 1"
cmd1.ExecuteNonQuery()
' Commit the changes to disk if everything above succeeded
'
tx.Commit()
Catch
tx.Rollback()
Finally
conn.Close()
End Try
SqlCeConnection conn = new SqlCeConnection("Data Source = MyDatabase.sdf; Password ='<pwd>'");
conn.Open();
// Start a local transaction
//
SqlCeTransaction tx = conn.BeginTransaction();
// By default, commands run in auto-commit mode;
//
SqlCeCommand cmd1 = conn.CreateCommand();
// You may create multiple commands on the same connection
//
SqlCeCommand cmd2 = conn.CreateCommand();
// To enlist a command in a transaction, set the Transaction property
//
cmd1.Transaction = tx;
try
{
cmd1.CommandText = "INSERT INTO Shippers ([Company Name]) VALUES ('Northwind Traders')";
cmd1.ExecuteNonQuery();
// Auto-commited because cmd2 is not enlisted in a transaction
//
cmd2.CommandText = "INSERT INTO Employees ([Last Name], [First Name]) VALUES ('Decker', 'Barbara')";
cmd2.ExecuteNonQuery();
// This will cause referential constraint violation
//
cmd1.CommandText = "DELETE FROM Products WHERE [Product ID] = 1";
cmd1.ExecuteNonQuery();
// Commit the changes to disk if everything above succeeded
//
tx.Commit();
}
catch (Exception)
{
tx.Rollback();
}
finally
{
conn.Close();
}