Database

Transactions

Methods for wrapping multiple database operations in a single transaction with commit and rollback support.

Methods for wrapping multiple database operations in a single transaction with commit and rollback support.

BeginTransaction starts a transaction on the current connection. Subsequent operations (Create, Update, Delete) participate in the transaction. Commit permanently applies all changes made since BeginTransaction. Rollback undoes all changes since the last BeginTransaction. Use these when you need to execute multiple statements atomically - if any step fails, roll back to leave the database unchanged.

Examples

GPAL Fluent: High-level fluent C# API

//BeginTransaction, Commit, and Rollback are properties that execute immediately when accessed. Store the database object with ToGPALObject() when you need to reference it across multiple statement calls.

// Wrap multiple inserts in a transaction var db = GPAL.Database .WithConnectionString(connStr) .ToGPALObject(); db.BeginTransaction .WithSQLCommand("INSERT INTO Orders (CustomerId, Total) VALUES (@cid, @total)") .WithParameter(customerId) .WithParameter(orderTotal) .Create .Execute(out _); // If all steps succeed: db.Commit; // On error: // db.Rollback;