共用方式為


SqlCeCommand.Transaction Property

取得或設定在其中執行 SqlCeCommand 的交易。

命名空間: System.Data.SqlServerCe
組件: System.Data.SqlServerCe (在 system.data.sqlserverce.dll)

語法

'宣告
Public Property Transaction As SqlCeTransaction
public SqlCeTransaction Transaction { get; set; }
public:
property SqlCeTransaction^ Transaction {
    SqlCeTransaction^ get ();
    void set (SqlCeTransaction^ value);
}
/** @property */
public SqlCeTransaction get_Transaction ()

/** @property */
public void set_Transaction (SqlCeTransaction value)
public function get Transaction () : SqlCeTransaction

public function set Transaction (value : SqlCeTransaction)

屬性值

SqlCeTransaction。預設值為 null

備註

如果 Transaction 屬性已經設定為特定值且命令正在執行中時,就無法設定該屬性。如果設定 SqlCeTransaction 物件的交易屬性,而該物件未與 SqlCeCommand 物件連接到相同的 SqlCeConnection,則在下次執行陳述式時就會擲回例外狀況。

如果 SqlCeCommandSqlCeTransaction 設定為 null 的狀況下執行,此命令就會以自動認可模式執行;因此,每個陳述式都會在自己的交易中執行。

範例

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();
}

執行緒安全性

任何公用靜態 (共用 在 Microsoft Visual Basic) 此型別的成員具備執行緒安全。不保證任何執行個體成員安全執行緒。

平台

開發平台

Windows Vista, Windows Mobile 5.0, Windows XP Professional with Service Pack 2 (SP2), Windows Server 2003, Windows Mobile 2003 for Pocket PC, Windows CE 5.0
版本資訊
.NET Framework 及 NET Compact Framework
支援於 3.5
.NET Framework
支援於 3.0
.NET Compact Framework 及 .Net Framework
支援於 2.0

另請參閱

參考

SqlCeCommand Class
SqlCeCommand Members
System.Data.SqlServerCe Namespace