Compartilhar via


Método SqlCeConnection.BeginTransaction

Inicia uma transação do banco de dados.

Namespace:  System.Data.SqlServerCe
Assembly:  System.Data.SqlServerCe (em System.Data.SqlServerCe.dll)

Sintaxe

'Declaração
Public Function BeginTransaction As SqlCeTransaction
'Uso
Dim instance As SqlCeConnection
Dim returnValue As SqlCeTransaction

returnValue = instance.BeginTransaction()
public SqlCeTransaction BeginTransaction()
public:
SqlCeTransaction^ BeginTransaction()
member BeginTransaction : unit -> SqlCeTransaction 
public function BeginTransaction() : SqlCeTransaction

Valor de retorno

Tipo: System.Data.SqlServerCe.SqlCeTransaction
Um objeto que representa a nova transação.

Comentários

Confirme ou reverta a conexão explicitamente, usando o método Commit ou Rollback.

Exemplos

O exemplo a seguir cria uma SqlCeConnection e uma SqlCeTransaction e, em seguida, demonstra como usar os métodos BeginTransaction, Commit e Rollback.

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

Consulte também

Referência

SqlCeConnection Classe

Sobrecarregado BeginTransaction

Namespace System.Data.SqlServerCe