Recherche…


Syntaxe

  • conn.Execute (sql, transaction: tran); // spécifie le paramètre par nom
  • conn.Execute (sql, paramètres, tran);
  • conn.Query (sql, transaction: tran);
  • conn.Query (sql, paramètres, tran);
  • attend conn.ExecuteAsync (sql, transaction: tran); // Async
  • attendez conn.ExecuteAsync (sql, paramètres, tran);
  • attendez conn.QueryAsync (sql, transaction: tran);
  • attendez conn.QueryAsync (sql, paramètres, tran);

Utiliser une transaction

Cet exemple utilise SqlConnection, mais toute IDbConnection est prise en charge.

De plus, toute IDbTransaction est prise en charge à partir de la IDbConnection associée.

public void UpdateWidgetQuantity(int widgetId, int quantity)
{
    using(var conn = new SqlConnection("{connection string}")) {
        conn.Open();

        // create the transaction
        // You could use `var` instead of `SqlTransaction`
        using(SqlTransaction tran = conn.BeginTransaction()) {
            try
            {
                var sql = "update Widget set Quantity = @quantity where WidgetId = @id";
                var parameters = new { id = widgetId, quantity };

                // pass the transaction along to the Query, Execute, or the related Async methods.
                conn.Execute(sql, parameters, tran);

                // if it was successful, commit the transaction
                tran.Commit();
            }
            catch(Exception ex)
            {
                // roll the transaction back
                tran.Rollback();

                // handle the error however you need to.
                throw;
            }
        }
    }   
}

Accélérer les inserts

Enrouler un groupe d'insertions dans une transaction les accélérera en fonction de cette question / réponse StackOverflow .

Vous pouvez utiliser cette technique ou vous pouvez utiliser Bulk Copy pour accélérer une série d'opérations connexes à effectuer.

// Widget has WidgetId, Name, and Quantity properties
public void InsertWidgets(IEnumerable<Widget> widgets)
{
    using(var conn = new SqlConnection("{connection string}")) {
        conn.Open();

        using(var tran = conn.BeginTransaction()) {
            try
            {
                var sql = "insert Widget (WidgetId,Name,Quantity) Values(@WidgetId, @Name, @Quantity)";
                conn.Execute(sql, widgets, tran);
                tran.Commit();
            }
            catch(Exception ex)
            {
                tran.Rollback();
                // handle the error however you need to.
                throw;
            }
        }
    }   
}


Modified text is an extract of the original Stack Overflow Documentation
Sous licence CC BY-SA 3.0
Non affilié à Stack Overflow