SqlCeCommand.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 。預設值為 nullnull 參考 (在 Visual Basic 中為 Nothing)。
備註
如果 Transaction 屬性已經設定為特定值且命令正在執行中時,就無法設定該屬性。如果設定 SqlCeTransaction 物件的交易屬性,而該物件未與 SqlCeCommand 物件連接到相同的 SqlCeConnection,則在下次執行陳述式時就會擲回例外狀況。
如果 SqlCeCommand 在 SqlCeTransaction 設定為 nullnull 參考 (在 Visual Basic 中為 Nothing) 的狀況下執行,此命令就會以自動認可模式執行;因此,每個陳述式都會在自己的交易中執行。
範例
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();
}