Condividi tramite


SqlCeCommand.Transaction Property

Gets or sets the transaction in which the SqlCeCommand executes.

Namespace: System.Data.SqlServerCe
Assembly: System.Data.SqlServerCe (in system.data.sqlserverce.dll)

Syntax

'Declaration
Public Property Transaction As SqlCeTransaction
'Usage
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);
}
/** @property */
public SqlCeTransaction get_Transaction ()

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

public function set Transaction (value : SqlCeTransaction)
Not applicable.

Property Value

The SqlCeTransaction. The default value is a null reference (Nothing in Visual Basic).

Remarks

You cannot set the Transaction property if it is already set to a specific value and the command is in the process of executing. If you set the transaction property of a SqlCeTransaction object that is not connected to the same SqlCeConnection as the SqlCeCommand object, an exception will be thrown the next time you execute a statement.

If SqlCeCommand runs with SqlCeTransaction set to a null reference (Nothing in Visual Basic), the command runs in autocommit mode; therefore, each statement runs in its own transaction.

Example

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 ('Nancy', 'Smith')"
    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 ('Nancy', 'Smith')";
    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();
}

Platforms

Windows CE, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows XP Professional x64 Edition, Windows XP SP2

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

Version Information

.NET Framework

Supported in: 3.0

.NET Compact Framework

Supported in: 2.0, 1.0

See Also

Reference

SqlCeCommand Class
SqlCeCommand Members
System.Data.SqlServerCe Namespace