Questo articolo è stato tradotto automaticamente. Per visualizzare l'articolo in inglese, selezionare la casella di controllo Inglese. È possibile anche visualizzare il testo inglese in una finestra popup posizionando il puntatore del mouse sopra il testo.

Metodo SqlConnection.BeginTransaction ()


Data di pubblicazione: ottobre 2016

Avvia una transazione di database.

Spazio dei nomi:   System.Data.SqlClient
Assembly:  System.Data (in System.Data.dll)

public SqlTransaction BeginTransaction()

Valore restituito

Type: System.Data.SqlClient.SqlTransaction

Oggetto che rappresenta la nuova transazione.

Exception Condition

Le transazioni parallele non sono consentite quando si usa MARS (Multiple Active Result Sets).


Le transazioni parallele non sono supportate.

This command maps to the ssNoVersion implementation of BEGIN TRANSACTION.

You must explicitly commit or roll back the transaction using the M:System.Data.SqlClient.SqlTransaction.Commit or M:System.Data.SqlClient.SqlTransaction.Rollback method. To make sure that the dnprdnshort Data Provider for ssNoVersion transaction management model performs correctly, avoid using other transaction management models, such as the one provided by ssNoVersion.


If you do not specify an isolation level, the default isolation level is used. To specify an isolation level with the Overload:System.Data.SqlClient.SqlConnection.BeginTransaction method, use the overload that takes the iso parameter (M:System.Data.SqlClient.SqlConnection.BeginTransaction(System.Data.IsolationLevel)). The isolation level set for a transaction persists after the transaction is completed and until the connection is closed or disposed. Setting the isolation level to Snapshot in a database where the snapshot isolation level is not enabled does not throw an exception. The transaction will complete using the default isolation level.


If a transaction is started and a level 16 or higher error occurs on the server, the transaction will not be rolled back until the M:System.Data.SqlClient.SqlDataReader.Read method is invoked. No exception is thrown on ExecuteReader.


When your query returns a large amount of data and calls BeginTransaction, a T:System.Data.SqlClient.SqlException is thrown because ssNoVersion does not allow parallel transactions when using MARS. To avoid this problem, always associate a transaction with the command, the connection, or both before any readers are open.

For more information on ssNoVersion transactions, see "Explicit Transactions" and "Coding Efficient Transactions" in ssNoVersion Books Online.

The following example creates a T:System.Data.SqlClient.SqlConnection and a T:System.Data.SqlClient.SqlTransaction. It also demonstrates how to use the M:System.Data.SqlClient.SqlConnection.BeginTransaction, a M:System.Data.SqlClient.SqlTransaction.Commit, and M:System.Data.SqlClient.SqlTransaction.Rollback methods.

private static void ExecuteSqlTransaction(string connectionString)
    using (SqlConnection connection = new SqlConnection(connectionString))

        SqlCommand command = connection.CreateCommand();
        SqlTransaction transaction;

        // Start a local transaction.
        transaction = connection.BeginTransaction("SampleTransaction");

        // Must assign both transaction object and connection
        // to Command object for a pending local transaction
        command.Connection = connection;
        command.Transaction = transaction;

            command.CommandText =
                "Insert into Region (RegionID, RegionDescription) VALUES (100, 'Description')";
            command.CommandText =
                "Insert into Region (RegionID, RegionDescription) VALUES (101, 'Description')";

            // Attempt to commit the transaction.
            Console.WriteLine("Both records are written to database.");
        catch (Exception ex)
            Console.WriteLine("Commit Exception Type: {0}", ex.GetType());
            Console.WriteLine("  Message: {0}", ex.Message);

            // Attempt to roll back the transaction.
            catch (Exception ex2)
                // This catch block will handle any errors that may have occurred
                // on the server that would cause the rollback to fail, such as
                // a closed connection.
                Console.WriteLine("Rollback Exception Type: {0}", ex2.GetType());
                Console.WriteLine("  Message: {0}", ex2.Message);

.NET Framework
Disponibile da 1.1
Torna all'inizio